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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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." )
53 set_shortname( _("RTSP VoD" ) );
54 set_description( _("RTSP VoD server") );
55 set_category( CAT_SOUT );
56 set_subcategory( SUBCAT_SOUT_VOD );
57 set_capability( "vod server", 1 );
58 set_callbacks( Open, Close );
59 add_shortcut( "rtsp" );
60 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
63 /*****************************************************************************
65 *****************************************************************************/
67 typedef struct media_es_t media_es_t;
71 media_es_t *p_media_es;
80 int64_t i_last; /* for timeout */
82 vlc_bool_t b_playing; /* is it in "play" state */
83 vlc_bool_t b_paused; /* is it in "pause" state */
86 rtsp_client_es_t **es;
96 httpd_url_t *p_rtsp_url;
102 uint8_t i_payload_type;
114 httpd_url_t *p_rtsp_url;
115 char *psz_rtsp_control_v4, *psz_rtsp_control_v6;
127 vlc_bool_t b_multicast;
139 rtsp_client_t **rtsp;
142 char *psz_session_name;
143 char *psz_session_description;
144 char *psz_session_url;
145 char *psz_session_email;
152 httpd_host_t *p_rtsp_host;
161 static vod_media_t *MediaNew( vod_t *, const 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( const vod_media_t *, httpd_client_t *cl );
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.i_port <= 0 ) url.i_port = 554;
206 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
207 if( !p_sys ) goto error;
208 p_sys->p_rtsp_host = 0;
211 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
212 if( !p_sys->p_rtsp_host )
214 msg_Err( p_vod, "cannot create http server (%s:%i)",
215 url.psz_host, url.i_port );
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 */
252 free( p_sys->psz_path );
256 /*****************************************************************************
258 *****************************************************************************/
259 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
260 input_item_t *p_item )
262 vod_sys_t *p_sys = p_vod->p_sys;
263 vod_media_t *p_media = malloc( sizeof(vod_media_t) );
268 msg_Err( p_vod, "not enough memory" );
272 memset( p_media, 0, sizeof(vod_media_t) );
274 p_media->psz_mux = 0;
276 p_media->b_raw = VLC_FALSE;
278 asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
279 p_media->p_rtsp_url =
280 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
283 if( !p_media->p_rtsp_url )
285 msg_Err( p_vod, "cannot create http url (%s)", p_media->psz_rtsp_path);
286 free( p_media->psz_rtsp_path );
291 msg_Dbg( p_vod, "created rtsp url: %s", p_media->psz_rtsp_path );
293 asprintf( &p_media->psz_rtsp_control_v4,
294 "a=control:rtsp://%%s:%d%s/trackid=%%d\r\n",
295 p_sys->i_port, p_media->psz_rtsp_path );
296 asprintf( &p_media->psz_rtsp_control_v6,
297 "a=control:rtsp://[%%s]:%d%s/trackid=%%d\r\n",
298 p_sys->i_port, p_media->psz_rtsp_path );
300 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
301 RtspCallback, (void*)p_media );
302 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
303 RtspCallback, (void*)p_media );
304 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
305 RtspCallback, (void*)p_media );
306 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
307 RtspCallback, (void*)p_media );
308 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
309 RtspCallback, (void*)p_media );
311 p_media->p_vod = p_vod;
313 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
315 vlc_mutex_init( p_vod, &p_media->lock );
316 p_media->psz_session_name = strdup("");
317 p_media->psz_session_description = strdup("");
318 p_media->psz_session_url = strdup("");
319 p_media->psz_session_email = strdup("");
321 p_media->i_port_audio = 1234;
322 p_media->i_port_video = 1236;
323 p_media->i_port = 1238;
324 p_media->i_payload_type = 96;
326 p_media->i_sdp_id = mdate();
327 p_media->i_sdp_version = 1;
328 p_media->i_length = p_item->i_duration;
330 vlc_mutex_lock( &p_item->lock );
331 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
332 for( i = 0; i < p_item->i_es; i++ )
334 MediaAddES( p_vod, p_media, p_item->es[i] );
336 vlc_mutex_unlock( &p_item->lock );
341 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
343 vod_sys_t *p_sys = p_vod->p_sys;
345 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
347 while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
348 httpd_UrlDelete( p_media->p_rtsp_url );
349 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
350 if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
351 if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
353 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
355 while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
357 vlc_mutex_destroy( &p_media->lock );
358 free( p_media->psz_session_name );
359 free( p_media->psz_session_description );
360 free( p_media->psz_session_url );
361 free( p_media->psz_session_email );
365 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
367 media_es_t *p_es = malloc( sizeof(media_es_t) );
370 memset( p_es, 0, sizeof(media_es_t) );
371 p_media->psz_mux = NULL;
373 /* TODO: update SDP, etc... */
374 asprintf( &psz_urlc, "%s/trackid=%d",
375 p_media->psz_rtsp_path, p_media->i_es );
376 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
378 switch( p_fmt->i_codec )
380 case VLC_FOURCC( 's', '1', '6', 'b' ):
381 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
383 p_es->i_payload_type = 11;
385 else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
387 p_es->i_payload_type = 10;
391 p_es->i_payload_type = p_media->i_payload_type++;
393 p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
394 sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
395 p_fmt->audio.i_channels );
397 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
398 p_es->i_payload_type = p_media->i_payload_type++;
399 p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
400 sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
401 p_fmt->audio.i_channels );
403 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
404 p_es->i_payload_type = 14;
405 p_es->psz_rtpmap = strdup( "MPA/90000" );
407 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
408 p_es->i_payload_type = 32;
409 p_es->psz_rtpmap = strdup( "MPV/90000" );
411 case VLC_FOURCC( 'a', '5', '2', ' ' ):
412 p_es->i_payload_type = p_media->i_payload_type++;
413 p_es->psz_rtpmap = strdup( "ac3/90000" );
415 case VLC_FOURCC( 'H', '2', '6', '3' ):
416 p_es->i_payload_type = p_media->i_payload_type++;
417 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
419 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
420 p_es->i_payload_type = p_media->i_payload_type++;
421 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
422 if( p_fmt->i_extra > 0 )
424 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
425 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
426 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
427 sprintf( p_es->psz_fmtp,
428 "profile-level-id=3; config=%s;", p_hexa );
432 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
433 p_es->i_payload_type = p_media->i_payload_type++;
434 p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
435 sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
436 if( p_fmt->i_extra > 0 )
438 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
439 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
440 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
441 sprintf( p_es->psz_fmtp,
442 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
443 "config=%s; SizeLength=13;IndexLength=3; "
444 "IndexDeltaLength=3; Profile=1;", p_hexa );
448 case VLC_FOURCC( 'm', 'p', '2', 't' ):
449 p_media->psz_mux = "ts";
450 p_es->i_payload_type = 33;
451 p_es->psz_rtpmap = strdup( "MP2T/90000" );
453 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
454 p_media->psz_mux = "ps";
455 p_es->i_payload_type = p_media->i_payload_type++;
456 p_es->psz_rtpmap = strdup( "MP2P/90000" );
458 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
459 p_es->i_payload_type = p_media->i_payload_type++;
460 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
461 "AMR/8000/2" : "AMR/8000" );
462 p_es->psz_fmtp = strdup( "octet-align=1" );
464 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
465 p_es->i_payload_type = p_media->i_payload_type++;
466 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
467 "AMR-WB/16000/2" : "AMR-WB/16000" );
468 p_es->psz_fmtp = strdup( "octet-align=1" );
472 msg_Err( p_vod, "cannot add this stream (unsupported "
473 "codec: %4.4s)", (char*)&p_fmt->i_codec );
479 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
482 if( !p_es->p_rtsp_url )
484 msg_Err( p_vod, "cannot create http url (%s)", psz_urlc );
491 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
492 RtspCallbackES, (void*)p_es );
493 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
494 RtspCallbackES, (void*)p_es );
495 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
496 RtspCallbackES, (void*)p_es );
497 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
498 RtspCallbackES, (void*)p_es );
500 es_format_Copy( &p_es->fmt, p_fmt );
502 p_es->p_media = p_media;
505 /* Choose the port */
506 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
508 p_es->i_port = p_media->i_port_audio;
509 p_media->i_port_audio = 0;
511 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
513 p_es->i_port = p_media->i_port_video;
514 p_media->i_port_video = 0;
516 while( !p_es->i_port )
518 if( p_media->i_port != p_media->i_port_audio &&
519 p_media->i_port != p_media->i_port_video )
521 p_es->i_port = p_media->i_port;
522 p_media->i_port += 2;
525 p_media->i_port += 2;
532 vlc_mutex_lock( &p_media->lock );
533 TAB_APPEND( p_media->i_es, p_media->es, p_es );
534 vlc_mutex_unlock( &p_media->lock );
536 p_media->i_sdp_version++;
541 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
543 media_es_t *p_es = 0;
547 for( i = 0; i < p_media->i_es; i++ )
549 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
550 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
551 p_media->es[i]->fmt.i_id == p_fmt->i_id )
553 p_es = p_media->es[i];
558 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
560 vlc_mutex_lock( &p_media->lock );
561 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
562 vlc_mutex_unlock( &p_media->lock );
564 if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
565 if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
566 p_media->i_sdp_version++;
568 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
569 es_format_Clean( &p_es->fmt );
572 /****************************************************************************
573 * RTSP server implementation
574 ****************************************************************************/
575 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
577 rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
578 memset( p_rtsp, 0, sizeof(rtsp_client_t) );
581 p_rtsp->psz_session = psz_session;
582 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
584 msg_Dbg( p_media->p_vod, "new session: %s", psz_session );
589 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
593 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
595 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
597 return p_media->rtsp[i];
604 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
606 msg_Dbg( p_media->p_vod, "closing session: %s", p_rtsp->psz_session );
608 while( p_rtsp->i_es-- )
610 if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
611 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
612 free( p_rtsp->es[p_rtsp->i_es] );
613 if( !p_rtsp->i_es ) free( p_rtsp->es );
616 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
618 free( p_rtsp->psz_session );
622 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
623 httpd_message_t *answer, httpd_message_t *query )
625 vod_media_t *p_media = (vod_media_t*)p_args;
626 vod_t *p_vod = p_media->p_vod;
627 char *psz_transport = NULL;
628 char *psz_playnow = NULL; /* support option: x-playNow */
629 char *psz_session = NULL;
630 rtsp_client_t *p_rtsp;
633 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
635 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
637 answer->i_proto = HTTPD_PROTO_RTSP;
638 answer->i_version = query->i_version;
639 answer->i_type = HTTPD_MSG_ANSWER;
641 answer->p_body = NULL;
643 switch( query->i_type )
645 case HTTPD_MSG_SETUP:
647 psz_playnow = httpd_MsgGet( query, "x-playNow" );
648 psz_transport = httpd_MsgGet( query, "Transport" );
649 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
651 if( strstr( psz_transport, "unicast" ) &&
652 strstr( psz_transport, "client_port=" ) )
654 rtsp_client_t *p_rtsp;
655 char ip[NI_MAXNUMERICHOST];
656 i_port = atoi( strstr( psz_transport, "client_port=" ) +
657 strlen("client_port=") );
659 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
660 strstr( psz_transport, "RAW/RAW/UDP" ) )
662 p_media->b_raw = VLC_TRUE;
665 if( httpd_ClientIP( cl, ip ) == NULL )
667 answer->i_status = 500;
668 answer->psz_status = strdup( "Internal server error" );
670 answer->p_body = NULL;
674 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
677 psz_session = httpd_MsgGet( query, "Session" );
678 if( !psz_session || !*psz_session )
680 asprintf( &psz_session, "%d", rand() );
681 p_rtsp = RtspClientNew( p_media, psz_session );
685 p_rtsp = RtspClientGet( p_media, psz_session );
688 /* FIXME right error code */
689 answer->i_status = 454;
690 answer->psz_status = strdup( "Unknown session id" );
692 answer->p_body = NULL;
697 answer->i_status = 200;
698 answer->psz_status = strdup( "OK" );
700 answer->p_body = NULL;
704 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
706 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
707 i_port, i_port + 1 );
709 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
711 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
712 i_port, i_port + 1 );
716 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
717 i_port, i_port + 1 );
719 else /* TODO strstr( psz_transport, "interleaved" ) ) */
721 answer->i_status = 461;
722 answer->psz_status = strdup( "Unsupported Transport" );
724 answer->p_body = NULL;
733 char *psz_output, ip[NI_MAXNUMERICHOST];
734 int i, i_port_audio = 0, i_port_video = 0;
736 /* for now only multicast so easy */
739 answer->i_status = 200;
740 answer->psz_status = strdup( "OK" );
742 answer->p_body = NULL;
746 psz_session = httpd_MsgGet( query, "Session" );
747 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
749 p_rtsp = RtspClientGet( p_media, psz_session );
752 if( p_rtsp->b_playing && p_rtsp->b_paused )
754 vod_MediaControl( p_vod, p_media, psz_session,
756 p_rtsp->b_paused = VLC_FALSE;
759 else if( p_rtsp->b_playing ) break;
761 if( httpd_ClientIP( cl, ip ) == NULL ) break;
763 p_rtsp->b_playing = VLC_TRUE;
765 /* FIXME for != 1 video and 1 audio */
766 for( i = 0; i < p_rtsp->i_es; i++ )
768 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
769 i_port_audio = p_rtsp->es[i]->i_port;
770 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
771 i_port_video = p_rtsp->es[i]->i_port;
774 if( p_media->psz_mux )
778 asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
779 ip, i_port, p_media->psz_mux );
783 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
784 ip, i_port_video, p_media->psz_mux );
789 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
790 "port-audio=%i}", ip, i_port_video, i_port_audio );
793 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
799 case HTTPD_MSG_DESCRIBE:
802 SDPGenerate( p_media, cl );
804 if( psz_sdp != NULL )
806 answer->i_status = 200;
807 answer->psz_status = strdup( "OK" );
808 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
810 answer->p_body = (uint8_t *)psz_sdp;
811 answer->i_body = strlen( psz_sdp );
815 answer->i_status = 500;
816 answer->psz_status = strdup( "Internal server error" );
817 answer->p_body = NULL;
823 case HTTPD_MSG_PAUSE:
824 psz_session = httpd_MsgGet( query, "Session" );
825 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
827 p_rtsp = RtspClientGet( p_media, psz_session );
830 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
831 p_rtsp->b_paused = VLC_TRUE;
833 answer->i_status = 200;
834 answer->psz_status = strdup( "OK" );
836 answer->p_body = NULL;
839 case HTTPD_MSG_TEARDOWN:
840 /* for now only multicast so easy again */
841 answer->i_status = 200;
842 answer->psz_status = strdup( "OK" );
844 answer->p_body = NULL;
846 psz_session = httpd_MsgGet( query, "Session" );
847 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
849 p_rtsp = RtspClientGet( p_media, psz_session );
852 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
853 RtspClientDel( p_media, p_rtsp );
860 httpd_MsgAdd( answer, "Server", "VLC Server" );
861 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
862 httpd_MsgAdd( answer, "Cseq", "%d",
863 atoi( httpd_MsgGet( query, "Cseq" ) ) );
864 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
868 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
874 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
875 httpd_message_t *answer, httpd_message_t *query )
877 media_es_t *p_es = (media_es_t*)p_args;
878 vod_media_t *p_media = p_es->p_media;
879 vod_t *p_vod = p_media->p_vod;
880 rtsp_client_t *p_rtsp = NULL;
881 char *psz_transport = NULL;
882 char *psz_playnow = NULL; /* support option: x-playNow */
883 char *psz_session = NULL;
884 char *psz_position = NULL;
887 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
889 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
891 answer->i_proto = HTTPD_PROTO_RTSP;
892 answer->i_version = query->i_version;
893 answer->i_type = HTTPD_MSG_ANSWER;
895 answer->p_body = NULL;
897 switch( query->i_type )
899 case HTTPD_MSG_SETUP:
900 psz_playnow = httpd_MsgGet( query, "x-playNow" );
901 psz_transport = httpd_MsgGet( query, "Transport" );
903 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
905 if( strstr( psz_transport, "unicast" ) &&
906 strstr( psz_transport, "client_port=" ) )
908 rtsp_client_t *p_rtsp;
909 rtsp_client_es_t *p_rtsp_es;
910 char ip[NI_MAXNUMERICHOST];
911 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
912 strlen("client_port=") );
914 if( httpd_ClientIP( cl, ip ) == NULL )
916 answer->i_status = 500;
917 answer->psz_status = strdup( "Internal server error" );
919 answer->p_body = NULL;
923 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
926 psz_session = httpd_MsgGet( query, "Session" );
927 if( !psz_session || !*psz_session )
929 asprintf( &psz_session, "%d", rand() );
930 p_rtsp = RtspClientNew( p_media, psz_session );
934 p_rtsp = RtspClientGet( p_media, psz_session );
937 /* FIXME right error code */
938 answer->i_status = 454;
939 answer->psz_status = strdup( "Unknown session id" );
941 answer->p_body = NULL;
946 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
947 p_rtsp_es->i_port = i_port;
948 p_rtsp_es->psz_ip = strdup( ip );
949 p_rtsp_es->p_media_es = p_es;
950 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
952 answer->i_status = 200;
953 answer->psz_status = strdup( "OK" );
955 answer->p_body = NULL;
959 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
961 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
962 i_port, i_port + 1 );
964 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
966 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
967 i_port, i_port + 1 );
972 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
973 i_port, i_port + 1 );
976 else /* TODO strstr( psz_transport, "interleaved" ) ) */
978 answer->i_status = 461;
979 answer->psz_status = strdup( "Unsupported Transport" );
981 answer->p_body = NULL;
988 /* This is kind of a kludge. Should we only support Aggregate
990 psz_session = httpd_MsgGet( query, "Session" );
991 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
993 p_rtsp = RtspClientGet( p_media, psz_session );
995 psz_position = httpd_MsgGet( query, "Range" );
996 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1001 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1004 if( sscanf( psz_position, "%f", &f_pos ) == 1 )
1006 f_pos /= ((float)(p_media->i_length/1000))/1000 / 100;
1007 vod_MediaControl( p_vod, p_media, psz_session,
1008 VOD_MEDIA_SEEK, (double)f_pos );
1014 answer->i_status = 200;
1015 answer->psz_status = strdup( "OK" );
1017 answer->p_body = NULL;
1021 case HTTPD_MSG_TEARDOWN:
1022 answer->i_status = 200;
1023 answer->psz_status = strdup( "OK" );
1025 answer->p_body = NULL;
1027 psz_session = httpd_MsgGet( query, "Session" );
1028 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1030 p_rtsp = RtspClientGet( p_media, psz_session );
1031 if( !p_rtsp ) break;
1033 for( i = 0; i < p_rtsp->i_es; i++ )
1035 if( p_rtsp->es[i]->p_media_es == p_es )
1037 if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1038 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1045 vod_MediaControl( p_vod, p_media, psz_session,
1047 RtspClientDel( p_media, p_rtsp );
1051 case HTTPD_MSG_PAUSE:
1052 /* This is kind of a kludge. Should we only support Aggregate
1054 psz_session = httpd_MsgGet( query, "Session" );
1055 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1057 p_rtsp = RtspClientGet( p_media, psz_session );
1058 if( !p_rtsp ) break;
1060 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1061 p_rtsp->b_paused = VLC_TRUE;
1063 answer->i_status = 200;
1064 answer->psz_status = strdup( "OK" );
1066 answer->p_body = NULL;
1070 return VLC_EGENERIC;
1074 httpd_MsgAdd( answer, "Server", "VLC Server" );
1075 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1076 httpd_MsgAdd( answer, "Cseq", "%d",
1077 atoi( httpd_MsgGet( query, "Cseq" ) ) );
1078 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1081 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1086 /*****************************************************************************
1088 * FIXME: need to be moved to a common place ?
1089 *****************************************************************************/
1090 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1093 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1094 const char *psz_control;
1096 if( httpd_ServerIP( cl, ip ) == NULL )
1099 p = strchr( ip, '%' );
1101 *p = '\0'; /* remove scope if present */
1103 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1105 /* Calculate size */
1106 i_size = sizeof( "v=0\r\n" ) +
1107 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1108 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1109 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1110 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1111 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1112 sizeof( "t=0 0\r\n" ) + /* FIXME */
1113 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1114 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1115 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1117 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1118 : p_media->psz_rtsp_control_v4;
1119 for( i = 0; i < p_media->i_es; i++ )
1121 media_es_t *p_es = p_media->es[i];
1123 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1124 if( p_es->psz_rtpmap )
1126 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1127 strlen( p_es->psz_rtpmap ) + 9;
1129 if( p_es->psz_fmtp )
1131 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1132 strlen( p_es->psz_fmtp ) + 9;
1135 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1137 p = psz_sdp = malloc( i_size );
1138 p += sprintf( p, "v=0\r\n" );
1139 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1140 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1141 if( *p_media->psz_session_name )
1142 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1143 if( *p_media->psz_session_description )
1144 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1145 if( *p_media->psz_session_url )
1146 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1147 if( *p_media->psz_session_email )
1148 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1150 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1151 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1153 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1155 if( p_media->i_length > 0 )
1156 p += sprintf( p, "a=range:npt=0-%.3f\r\n",
1157 ((float)(p_media->i_length/1000))/1000 );
1159 for( i = 0; i < p_media->i_es; i++ )
1161 media_es_t *p_es = p_media->es[i];
1163 if( p_es->fmt.i_cat == AUDIO_ES )
1165 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1166 p_es->i_port, p_es->i_payload_type );
1168 else if( p_es->fmt.i_cat == VIDEO_ES )
1170 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1171 p_es->i_port, p_es->i_payload_type );
1178 if( p_es->psz_rtpmap )
1180 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1183 if( p_es->psz_fmtp )
1185 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1189 p += sprintf( p, psz_control, ip, i );