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;
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;
160 static vod_media_t *MediaNew( vod_t *, const 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_path = strdup( url.psz_path ? url.psz_path : "/" );
219 p_sys->i_port = url.i_port;
221 vlc_UrlClean( &url );
225 p_vod->pf_media_new = MediaNew;
226 p_vod->pf_media_del = MediaDel;
227 p_vod->pf_media_add_es = MediaAddES;
228 p_vod->pf_media_del_es = MediaDelES;
234 if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
235 if( p_sys ) free( p_sys );
236 vlc_UrlClean( &url );
240 /*****************************************************************************
242 *****************************************************************************/
243 static void Close( vlc_object_t * p_this )
245 vod_t *p_vod = (vod_t *)p_this;
246 vod_sys_t *p_sys = p_vod->p_sys;
248 httpd_HostDelete( p_sys->p_rtsp_host );
250 /* TODO delete medias */
251 free( p_sys->psz_path );
255 /*****************************************************************************
257 *****************************************************************************/
258 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
259 input_item_t *p_item )
261 vod_sys_t *p_sys = p_vod->p_sys;
262 vod_media_t *p_media = malloc( sizeof(vod_media_t) );
267 msg_Err( p_vod, "not enough memory" );
271 memset( p_media, 0, sizeof(vod_media_t) );
273 p_media->psz_mux = 0;
276 asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
277 p_media->p_rtsp_url =
278 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
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_v4,
292 "a=control:rtsp://%%s:%d%s/trackid=%%d\r\n",
293 p_sys->i_port, p_media->psz_rtsp_path );
294 asprintf( &p_media->psz_rtsp_control_v6,
295 "a=control:rtsp://[%%s]:%d%s/trackid=%%d\r\n",
296 p_sys->i_port, p_media->psz_rtsp_path );
298 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
299 RtspCallback, (void*)p_media );
300 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
301 RtspCallback, (void*)p_media );
302 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
303 RtspCallback, (void*)p_media );
304 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
305 RtspCallback, (void*)p_media );
306 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
307 RtspCallback, (void*)p_media );
309 p_media->p_vod = p_vod;
311 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
313 vlc_mutex_init( p_vod, &p_media->lock );
314 p_media->psz_session_name = strdup("");
315 p_media->psz_session_description = strdup("");
316 p_media->psz_session_url = strdup("");
317 p_media->psz_session_email = strdup("");
319 p_media->i_port_audio = 1234;
320 p_media->i_port_video = 1236;
321 p_media->i_port = 1238;
322 p_media->i_payload_type = 96;
324 p_media->i_sdp_id = mdate();
325 p_media->i_sdp_version = 1;
326 p_media->i_length = p_item->i_duration;
328 vlc_mutex_lock( &p_item->lock );
329 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
330 for( i = 0; i < p_item->i_es; i++ )
332 MediaAddES( p_vod, p_media, p_item->es[i] );
334 vlc_mutex_unlock( &p_item->lock );
339 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
341 vod_sys_t *p_sys = p_vod->p_sys;
343 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
345 while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
346 httpd_UrlDelete( p_media->p_rtsp_url );
347 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
348 if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
349 if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
351 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
353 while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
355 vlc_mutex_destroy( &p_media->lock );
356 free( p_media->psz_session_name );
357 free( p_media->psz_session_description );
358 free( p_media->psz_session_url );
359 free( p_media->psz_session_email );
363 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
365 media_es_t *p_es = malloc( sizeof(media_es_t) );
368 memset( p_es, 0, sizeof(media_es_t) );
369 p_media->psz_mux = NULL;
371 /* TODO: update SDP, etc... */
372 asprintf( &psz_urlc, "%s/trackid=%d",
373 p_media->psz_rtsp_path, p_media->i_es );
374 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
376 switch( p_fmt->i_codec )
378 case VLC_FOURCC( 's', '1', '6', 'b' ):
379 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
381 p_es->i_payload_type = 11;
383 else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
385 p_es->i_payload_type = 10;
389 p_es->i_payload_type = p_media->i_payload_type++;
391 p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
392 sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
393 p_fmt->audio.i_channels );
395 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
396 p_es->i_payload_type = p_media->i_payload_type++;
397 p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
398 sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
399 p_fmt->audio.i_channels );
401 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
402 p_es->i_payload_type = 14;
403 p_es->psz_rtpmap = strdup( "MPA/90000" );
405 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
406 p_es->i_payload_type = 32;
407 p_es->psz_rtpmap = strdup( "MPV/90000" );
409 case VLC_FOURCC( 'a', '5', '2', ' ' ):
410 p_es->i_payload_type = p_media->i_payload_type++;
411 p_es->psz_rtpmap = strdup( "ac3/90000" );
413 case VLC_FOURCC( 'H', '2', '6', '3' ):
414 p_es->i_payload_type = p_media->i_payload_type++;
415 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
417 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
418 p_es->i_payload_type = p_media->i_payload_type++;
419 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
420 if( p_fmt->i_extra > 0 )
422 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
423 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
424 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
425 sprintf( p_es->psz_fmtp,
426 "profile-level-id=3; config=%s;", p_hexa );
430 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
431 p_es->i_payload_type = p_media->i_payload_type++;
432 p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
433 sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
434 if( p_fmt->i_extra > 0 )
436 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
437 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
438 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
439 sprintf( p_es->psz_fmtp,
440 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
441 "config=%s; SizeLength=13;IndexLength=3; "
442 "IndexDeltaLength=3; Profile=1;", p_hexa );
446 case VLC_FOURCC( 'm', 'p', '2', 't' ):
447 p_media->psz_mux = "ts";
448 p_es->i_payload_type = 33;
449 p_es->psz_rtpmap = strdup( "MP2T/90000" );
451 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
452 p_media->psz_mux = "ps";
453 p_es->i_payload_type = p_media->i_payload_type++;
454 p_es->psz_rtpmap = strdup( "MP2P/90000" );
456 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
457 p_es->i_payload_type = p_media->i_payload_type++;
458 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
459 "AMR/8000/2" : "AMR/8000" );
460 p_es->psz_fmtp = strdup( "octet-align=1" );
462 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
463 p_es->i_payload_type = p_media->i_payload_type++;
464 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
465 "AMR-WB/16000/2" : "AMR-WB/16000" );
466 p_es->psz_fmtp = strdup( "octet-align=1" );
470 msg_Err( p_vod, "cannot add this stream (unsupported "
471 "codec: %4.4s)", (char*)&p_fmt->i_codec );
477 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
480 if( !p_es->p_rtsp_url )
482 msg_Err( p_vod, "cannot create http url (%s)", psz_urlc );
489 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
490 RtspCallbackES, (void*)p_es );
491 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
492 RtspCallbackES, (void*)p_es );
493 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
494 RtspCallbackES, (void*)p_es );
495 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
496 RtspCallbackES, (void*)p_es );
498 es_format_Copy( &p_es->fmt, p_fmt );
500 p_es->p_media = p_media;
503 /* Choose the port */
504 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
506 p_es->i_port = p_media->i_port_audio;
507 p_media->i_port_audio = 0;
509 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
511 p_es->i_port = p_media->i_port_video;
512 p_media->i_port_video = 0;
514 while( !p_es->i_port )
516 if( p_media->i_port != p_media->i_port_audio &&
517 p_media->i_port != p_media->i_port_video )
519 p_es->i_port = p_media->i_port;
520 p_media->i_port += 2;
523 p_media->i_port += 2;
530 vlc_mutex_lock( &p_media->lock );
531 TAB_APPEND( p_media->i_es, p_media->es, p_es );
532 vlc_mutex_unlock( &p_media->lock );
534 p_media->i_sdp_version++;
539 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
541 media_es_t *p_es = 0;
545 for( i = 0; i < p_media->i_es; i++ )
547 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
548 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
549 p_media->es[i]->fmt.i_id == p_fmt->i_id )
551 p_es = p_media->es[i];
556 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
558 vlc_mutex_lock( &p_media->lock );
559 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
560 vlc_mutex_unlock( &p_media->lock );
562 if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
563 if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
564 p_media->i_sdp_version++;
566 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
567 es_format_Clean( &p_es->fmt );
570 /****************************************************************************
571 * RTSP server implementation
572 ****************************************************************************/
573 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
575 rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
576 memset( p_rtsp, 0, sizeof(rtsp_client_t) );
579 p_rtsp->psz_session = psz_session;
580 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
582 msg_Dbg( p_media->p_vod, "new session: %s", psz_session );
587 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
591 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
593 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
595 return p_media->rtsp[i];
602 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
604 msg_Dbg( p_media->p_vod, "closing session: %s", p_rtsp->psz_session );
606 while( p_rtsp->i_es-- )
608 if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
609 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
610 free( p_rtsp->es[p_rtsp->i_es] );
611 if( !p_rtsp->i_es ) free( p_rtsp->es );
614 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
616 free( p_rtsp->psz_session );
620 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
621 httpd_message_t *answer, httpd_message_t *query )
623 vod_media_t *p_media = (vod_media_t*)p_args;
624 vod_t *p_vod = p_media->p_vod;
625 char *psz_transport = NULL;
626 char *psz_playnow = NULL; /* support option: x-playNow */
627 char *psz_session = NULL;
628 rtsp_client_t *p_rtsp;
630 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
632 msg_Info( p_vod, "RtspCallback query: type=%d", query->i_type );
634 answer->i_proto = HTTPD_PROTO_RTSP;
635 answer->i_version = query->i_version;
636 answer->i_type = HTTPD_MSG_ANSWER;
638 switch( query->i_type )
640 case HTTPD_MSG_SETUP:
642 psz_playnow = httpd_MsgGet( query, "x-playNow" );
643 psz_transport = httpd_MsgGet( query, "Transport" );
644 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
646 if( strstr( psz_transport, "unicast" ) &&
647 strstr( psz_transport, "client_port=" ) )
649 rtsp_client_t *p_rtsp;
650 char ip[NI_MAXNUMERICHOST];
651 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
652 strlen("client_port=") );
654 if( httpd_ClientIP( cl, ip ) == NULL )
656 answer->i_status = 500;
657 answer->psz_status = strdup( "Internal server error" );
659 answer->p_body = NULL;
663 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
666 psz_session = httpd_MsgGet( query, "Session" );
667 if( !psz_session || !*psz_session )
669 asprintf( &psz_session, "%d", rand() );
670 p_rtsp = RtspClientNew( p_media, psz_session );
674 p_rtsp = RtspClientGet( p_media, psz_session );
677 /* FIXME right error code */
678 answer->i_status = 454;
679 answer->psz_status = strdup( "Unknown session id" );
681 answer->p_body = NULL;
686 if( psz_playnow ) /* support option: x-playNow */
689 answer->i_status = 200;
690 answer->psz_status = strdup( "OK" );
692 answer->p_body = NULL;
694 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
695 i_port, i_port + 1 );
697 else /* TODO strstr( psz_transport, "interleaved" ) ) */
699 answer->i_status = 461;
700 answer->psz_status = strdup( "Unsupported Transport" );
702 answer->p_body = NULL;
707 case HTTPD_MSG_DESCRIBE:
710 SDPGenerate( p_media, cl );
712 if( psz_sdp != NULL )
714 answer->i_status = 200;
715 answer->psz_status = strdup( "OK" );
716 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
718 answer->p_body = (uint8_t *)psz_sdp;
719 answer->i_body = strlen( psz_sdp );
723 answer->i_status = 500;
724 answer->psz_status = strdup( "Internal server error" );
725 answer->p_body = NULL;
733 rtsp_play_now: /* This avoids code duplications although it is ugly. */
735 char *psz_output, ip[NI_MAXNUMERICHOST];
736 int i, i_port_audio = 0, i_port_video = 0;
738 /* for now only multicast so easy */
739 answer->i_status = 200;
740 answer->psz_status = strdup( "OK" );
742 answer->p_body = NULL;
744 psz_session = httpd_MsgGet( query, "Session" );
745 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
747 p_rtsp = RtspClientGet( p_media, psz_session );
750 if( p_rtsp->b_playing && p_rtsp->b_paused )
752 vod_MediaControl( p_vod, p_media, psz_session,
754 p_rtsp->b_paused = VLC_FALSE;
757 else if( p_rtsp->b_playing ) break;
759 if( httpd_ClientIP( cl, ip ) == NULL ) break;
761 p_rtsp->b_playing = VLC_TRUE;
763 /* FIXME for != 1 video and 1 audio */
764 for( i = 0; i < p_rtsp->i_es; i++ )
766 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
767 i_port_audio = p_rtsp->es[i]->i_port;
768 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
769 i_port_video = p_rtsp->es[i]->i_port;
772 if( p_media->psz_mux )
774 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
775 ip, i_port_video, p_media->psz_mux );
779 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
780 "port-audio=%i}", ip, i_port_video, i_port_audio );
783 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
789 case HTTPD_MSG_PAUSE:
790 psz_session = httpd_MsgGet( query, "Session" );
791 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
793 p_rtsp = RtspClientGet( p_media, psz_session );
796 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
797 p_rtsp->b_paused = VLC_TRUE;
799 answer->i_status = 200;
800 answer->psz_status = strdup( "OK" );
802 answer->p_body = NULL;
805 case HTTPD_MSG_TEARDOWN:
806 /* for now only multicast so easy again */
807 answer->i_status = 200;
808 answer->psz_status = strdup( "OK" );
810 answer->p_body = NULL;
812 psz_session = httpd_MsgGet( query, "Session" );
813 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
815 p_rtsp = RtspClientGet( p_media, psz_session );
818 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
819 RtspClientDel( p_media, p_rtsp );
826 httpd_MsgAdd( answer, "Server", "VLC Server" );
827 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
828 httpd_MsgAdd( answer, "Cseq", "%d",
829 atoi( httpd_MsgGet( query, "Cseq" ) ) );
830 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
834 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
840 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
841 httpd_message_t *answer, httpd_message_t *query )
843 media_es_t *p_es = (media_es_t*)p_args;
844 vod_media_t *p_media = p_es->p_media;
845 vod_t *p_vod = p_media->p_vod;
846 rtsp_client_t *p_rtsp = NULL;
847 char *psz_transport = NULL;
848 char *psz_playnow = NULL; /* support option: x-playNow */
849 char *psz_session = NULL;
850 char *psz_position = NULL;
853 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
855 msg_Info( p_vod, "RtspCallback query: type=%d", query->i_type );
857 answer->i_proto = HTTPD_PROTO_RTSP;
858 answer->i_version = query->i_version;
859 answer->i_type = HTTPD_MSG_ANSWER;
861 switch( query->i_type )
863 case HTTPD_MSG_SETUP:
864 psz_playnow = httpd_MsgGet( query, "x-playNow" );
865 psz_transport = httpd_MsgGet( query, "Transport" );
867 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
869 if( strstr( psz_transport, "unicast" ) &&
870 strstr( psz_transport, "client_port=" ) )
872 rtsp_client_t *p_rtsp;
873 rtsp_client_es_t *p_rtsp_es;
874 char ip[NI_MAXNUMERICHOST];
875 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
876 strlen("client_port=") );
878 if( httpd_ClientIP( cl, ip ) == NULL )
880 answer->i_status = 500;
881 answer->psz_status = strdup( "Internal server error" );
883 answer->p_body = NULL;
887 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
890 psz_session = httpd_MsgGet( query, "Session" );
891 if( !psz_session || !*psz_session )
893 asprintf( &psz_session, "%d", rand() );
894 p_rtsp = RtspClientNew( p_media, psz_session );
898 p_rtsp = RtspClientGet( p_media, psz_session );
901 /* FIXME right error code */
902 answer->i_status = 454;
903 answer->psz_status = strdup( "Unknown session id" );
905 answer->p_body = NULL;
910 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
911 p_rtsp_es->i_port = i_port;
912 p_rtsp_es->psz_ip = strdup( ip );
913 p_rtsp_es->p_media_es = p_es;
914 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
916 if( psz_playnow ) /* support option: x-playNow */
919 answer->i_status = 200;
920 answer->psz_status = strdup( "OK" );
922 answer->p_body = NULL;
924 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
925 i_port, i_port + 1 );
927 else /* TODO strstr( psz_transport, "interleaved" ) ) */
929 answer->i_status = 461;
930 answer->psz_status = strdup( "Unsupported Transport" );
932 answer->p_body = NULL;
936 case HTTPD_MSG_TEARDOWN:
937 answer->i_status = 200;
938 answer->psz_status = strdup( "OK" );
940 answer->p_body = NULL;
942 psz_session = httpd_MsgGet( query, "Session" );
943 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
945 p_rtsp = RtspClientGet( p_media, psz_session );
948 for( i = 0; i < p_rtsp->i_es; i++ )
950 if( p_rtsp->es[i]->p_media_es == p_es )
952 if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
953 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
960 vod_MediaControl( p_vod, p_media, psz_session,
962 RtspClientDel( p_media, p_rtsp );
967 /* This avoids code duplications although it is ugly. */
970 /* This is kind of a kludge. Should we only support Aggregate
972 psz_session = httpd_MsgGet( query, "Session" );
973 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
975 p_rtsp = RtspClientGet( p_media, psz_session );
977 psz_position = httpd_MsgGet( query, "Range" );
978 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
983 msg_Dbg( p_vod, "seeking request: %s", psz_position );
986 if( sscanf( psz_position, "%f", &f_pos ) == 1 )
988 f_pos /= ((float)(p_media->i_length/1000))/1000 / 100;
989 vod_MediaControl( p_vod, p_media, psz_session,
990 VOD_MEDIA_SEEK, (double)f_pos );
994 answer->i_status = 200;
995 answer->psz_status = strdup( "OK" );
997 answer->p_body = NULL;
1000 case HTTPD_MSG_PAUSE:
1001 /* This is kind of a kludge. Should we only support Aggregate
1003 psz_session = httpd_MsgGet( query, "Session" );
1004 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1006 p_rtsp = RtspClientGet( p_media, psz_session );
1007 if( !p_rtsp ) break;
1009 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1010 p_rtsp->b_paused = VLC_TRUE;
1012 answer->i_status = 200;
1013 answer->psz_status = strdup( "OK" );
1015 answer->p_body = NULL;
1019 return VLC_EGENERIC;
1023 httpd_MsgAdd( answer, "Server", "VLC Server" );
1024 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1025 httpd_MsgAdd( answer, "Cseq", "%d",
1026 atoi( httpd_MsgGet( query, "Cseq" ) ) );
1027 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1030 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1035 /*****************************************************************************
1037 * FIXME: need to be moved to a common place ?
1038 *****************************************************************************/
1039 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1042 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1043 const char *psz_control;
1045 if( httpd_ServerIP( cl, ip ) == NULL )
1048 p = strchr( ip, '%' );
1050 *p = '\0'; /* remove scope if present */
1052 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1054 /* Calculate size */
1055 i_size = sizeof( "v=0\r\n" ) +
1056 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1057 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1058 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1059 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1060 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1061 sizeof( "t=0 0\r\n" ) + /* FIXME */
1062 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1063 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1064 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1066 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1067 : p_media->psz_rtsp_control_v4;
1068 for( i = 0; i < p_media->i_es; i++ )
1070 media_es_t *p_es = p_media->es[i];
1072 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1073 if( p_es->psz_rtpmap )
1075 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1076 strlen( p_es->psz_rtpmap ) + 9;
1078 if( p_es->psz_fmtp )
1080 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1081 strlen( p_es->psz_fmtp ) + 9;
1084 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1086 p = psz_sdp = malloc( i_size );
1087 p += sprintf( p, "v=0\r\n" );
1088 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1089 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1090 if( *p_media->psz_session_name )
1091 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1092 if( *p_media->psz_session_description )
1093 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1094 if( *p_media->psz_session_url )
1095 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1096 if( *p_media->psz_session_email )
1097 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1099 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1100 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1102 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1104 if( p_media->i_length > 0 )
1105 p += sprintf( p, "a=range:npt=0-%.3f\r\n",
1106 ((float)(p_media->i_length/1000))/1000 );
1108 for( i = 0; i < p_media->i_es; i++ )
1110 media_es_t *p_es = p_media->es[i];
1112 if( p_es->fmt.i_cat == AUDIO_ES )
1114 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1115 p_es->i_port, p_es->i_payload_type );
1117 else if( p_es->fmt.i_cat == VIDEO_ES )
1119 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1120 p_es->i_port, p_es->i_payload_type );
1127 if( p_es->psz_rtpmap )
1129 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1132 if( p_es->psz_fmtp )
1134 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1138 p += sprintf( p, psz_control, ip, i );