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." )
51 set_description( _("RTSP VoD server") );
52 set_capability( "vod server", 1 );
53 set_callbacks( Open, Close );
54 add_shortcut( "rtsp" );
55 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
58 /*****************************************************************************
60 *****************************************************************************/
65 int64_t i_last; /* for timeout */
67 vlc_bool_t b_playing; /* is it in "play" state */
77 httpd_url_t *p_rtsp_url;
89 httpd_url_t *p_rtsp_url;
90 char *psz_rtsp_control;
93 char *psz_destination;
105 rtsp_client_t **rtsp;
111 httpd_host_t *p_rtsp_host;
121 static vod_media_t *MediaNew( vod_t *, char *, input_item_t * );
122 static void MediaDel( vod_t *, vod_media_t * );
123 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
124 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
126 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
127 static rtsp_client_t *RtspClientGet( vod_media_t *, char * );
128 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
130 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
131 httpd_message_t *, httpd_message_t * );
132 static int RtspCallbackId( httpd_callback_sys_t *, httpd_client_t *,
133 httpd_message_t *, httpd_message_t * );
135 static char *SDPGenerate( vod_media_t *, char * );
137 /*****************************************************************************
138 * Open: Starts the RTSP server module
139 *****************************************************************************/
140 static int Open( vlc_object_t *p_this )
142 vod_t *p_vod = (vod_t *)p_this;
143 vod_sys_t *p_sys = 0;
147 psz_url = config_GetPsz( p_vod, "rtsp-host" );
148 vlc_UrlParse( &url, psz_url, 0 );
149 if( psz_url ) free( psz_url );
151 if( !url.psz_host || !*url.psz_host )
153 if( url.psz_host ) free( url.psz_host );
154 url.psz_host = strdup( "localhost" );
156 if( url.i_port <= 0 ) url.i_port = 554;
158 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
159 if( !p_sys ) goto error;
160 p_sys->p_rtsp_host = 0;
163 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
164 if( !p_sys->p_rtsp_host )
166 msg_Err( p_vod, "cannot create http server (%s:%i)",
167 url.psz_host, url.i_port );
171 p_sys->psz_host = strdup( url.psz_host );
172 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
173 p_sys->i_port = url.i_port;
175 vlc_UrlClean( &url );
179 p_vod->pf_media_new = MediaNew;
180 p_vod->pf_media_del = MediaDel;
181 p_vod->pf_media_add_es = MediaAddES;
182 p_vod->pf_media_del_es = MediaDelES;
188 if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
189 if( p_sys ) free( p_sys );
190 vlc_UrlClean( &url );
194 /*****************************************************************************
196 *****************************************************************************/
197 static void Close( vlc_object_t * p_this )
199 vod_t *p_vod = (vod_t *)p_this;
200 vod_sys_t *p_sys = p_vod->p_sys;
202 httpd_HostDelete( p_sys->p_rtsp_host );
204 /* TODO delete medias */
209 /*****************************************************************************
211 *****************************************************************************/
212 static vod_media_t *MediaNew( vod_t *p_vod, char *psz_name,
213 input_item_t *p_item )
215 vod_sys_t *p_sys = p_vod->p_sys;
216 vod_media_t *p_media = malloc( sizeof(vod_media_t) );
217 memset( p_media, 0, sizeof(vod_media_t) );
219 asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
221 p_media->p_rtsp_url =
222 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, 0, 0 );
224 if( !p_media->p_rtsp_url )
226 msg_Err( p_vod, "cannot create http url" );
227 free( p_media->psz_rtsp_path );
232 msg_Dbg( p_vod, "created rtsp url: %s", p_media->psz_rtsp_path );
234 asprintf( &p_media->psz_rtsp_control, "rtsp://%s:%d%s",
235 p_sys->psz_host, p_sys->i_port, p_media->psz_rtsp_path );
237 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
238 RtspCallback, (void*)p_media );
239 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
240 RtspCallback, (void*)p_media );
241 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
242 RtspCallback, (void*)p_media );
243 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
244 RtspCallback, (void*)p_media );
246 p_media->p_vod = p_vod;
248 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
253 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
255 vod_sys_t *p_sys = p_vod->p_sys;
257 while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
258 httpd_UrlDelete( p_media->p_rtsp_url );
259 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
260 if( p_media->psz_rtsp_control ) free( p_media->psz_rtsp_control );
262 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
266 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
268 media_es_t *p_es = malloc( sizeof(media_es_t) );
269 memset( p_es, 0, sizeof(media_es_t) );
271 TAB_APPEND( p_media->i_es, p_media->es, p_es );
273 /* TODO: update SDP, etc... */
275 if( p_media->p_rtsp_url )
277 char psz_urlc[strlen( p_media->psz_rtsp_control ) + 1 + 10];
279 sprintf( psz_urlc, "%s/trackid=%d",
280 p_media->psz_rtsp_path, p_media->i_es );
281 fprintf( stderr, "rtsp: adding %s\n", psz_urlc );
284 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, 0, 0 );
286 if( p_es->p_rtsp_url )
288 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
289 RtspCallbackId, (void*)p_es );
291 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
292 RtspCallback, (void*)p_es );
293 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
294 RtspCallback, (void*)p_es );
300 p_es->p_media = p_media;
305 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
307 media_es_t *p_es = 0;
309 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
311 /* TODO do something useful */
313 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
316 /****************************************************************************
317 * RTSP server implementation
318 ****************************************************************************/
319 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
321 rtsp_client_t *rtsp = malloc( sizeof(rtsp_client_t) );
323 rtsp->psz_session = psz_session;
325 rtsp->b_playing = VLC_FALSE;
327 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, rtsp );
329 msg_Dbg( p_media->p_vod, "new session: %s", psz_session );
334 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
338 for( i = 0; i < p_media->i_rtsp; i++ )
340 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
342 return p_media->rtsp[i];
349 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *rtsp )
351 msg_Dbg( p_media->p_vod, "closing session: %s", rtsp->psz_session );
353 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, rtsp );
355 free( rtsp->psz_session );
359 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
360 httpd_message_t *answer, httpd_message_t *query )
362 vod_media_t *p_media = (vod_media_t*)p_args;
363 vod_t *p_vod = p_media->p_vod;
364 char *psz_destination = p_media->psz_destination;
365 char *psz_session = NULL;
367 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
369 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
371 answer->i_proto = HTTPD_PROTO_RTSP;
372 answer->i_version = query->i_version;
373 answer->i_type = HTTPD_MSG_ANSWER;
375 switch( query->i_type )
377 case HTTPD_MSG_DESCRIBE:
380 SDPGenerate( p_media, psz_destination ?
381 psz_destination : "0.0.0.0" );
383 answer->i_status = 200;
384 answer->psz_status = strdup( "OK" );
385 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
387 answer->p_body = psz_sdp;
388 answer->i_body = strlen( psz_sdp );
396 /* for now only multicast so easy */
397 answer->i_status = 200;
398 answer->psz_status = strdup( "OK" );
400 answer->p_body = NULL;
402 psz_session = httpd_MsgGet( query, "Session" );
403 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
405 rtsp = RtspClientGet( p_media, psz_session );
406 if( rtsp && !rtsp->b_playing )
408 rtsp->b_playing = VLC_TRUE;
409 /* TODO: do something useful */
414 case HTTPD_MSG_PAUSE:
415 psz_session = httpd_MsgGet( query, "Session" );
416 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
417 /* TODO: do something useful */
420 case HTTPD_MSG_TEARDOWN:
424 /* for now only multicast so easy again */
425 answer->i_status = 200;
426 answer->psz_status = strdup( "OK" );
428 answer->p_body = NULL;
430 psz_session = httpd_MsgGet( query, "Session" );
431 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
433 rtsp = RtspClientGet( p_media, psz_session );
436 /* TODO: do something useful */
437 RtspClientDel( p_media, rtsp );
446 httpd_MsgAdd( answer, "Server", "VLC Server" );
447 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
448 httpd_MsgAdd( answer, "Cseq", "%d",
449 atoi( httpd_MsgGet( query, "Cseq" ) ) );
450 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
454 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
460 static int RtspCallbackId( httpd_callback_sys_t *p_args, httpd_client_t *cl,
461 httpd_message_t *answer, httpd_message_t *query )
463 vod_media_t *p_media = (vod_media_t*)p_args;
464 vod_t *p_vod = p_media->p_vod;
465 char *psz_session = NULL;
466 char *psz_transport = NULL;
468 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
470 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
472 answer->i_proto = HTTPD_PROTO_RTSP;
473 answer->i_version = query->i_version;
474 answer->i_type = HTTPD_MSG_ANSWER;
476 switch( query->i_type )
478 case HTTPD_MSG_SETUP:
479 psz_transport = httpd_MsgGet( query, "Transport" );
480 fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
482 if( strstr( psz_transport, "multicast" ) && p_media->psz_destination )
484 fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
485 answer->i_status = 200;
486 answer->psz_status = strdup( "OK" );
488 answer->p_body = NULL;
490 psz_session = httpd_MsgGet( query, "Session" );
491 if( !psz_session || !*psz_session )
493 if( psz_session ) free( psz_session );
494 asprintf( &psz_session, "%d", rand() );
497 httpd_MsgAdd( answer, "Transport",
498 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
499 p_media->psz_destination, p_media->i_port,
500 p_media->i_port+1, p_media->i_ttl );
502 else if( strstr( psz_transport, "unicast" ) &&
503 strstr( psz_transport, "client_port=" ) )
505 rtsp_client_t *rtsp = NULL;
506 char *ip = httpd_ClientIP( cl );
507 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
508 strlen("client_port=") );
512 answer->i_status = 400;
513 answer->psz_status = strdup( "Internal server error" );
515 answer->p_body = NULL;
519 fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
522 psz_session = httpd_MsgGet( query, "Session" );
523 if( !psz_session || !*psz_session )
525 if( psz_session ) free( psz_session );
526 asprintf( &psz_session, "%d", rand() );
527 rtsp = RtspClientNew( p_media, psz_session );
531 rtsp = RtspClientGet( p_media, psz_session );
534 /* FIXME right error code */
535 answer->i_status = 400;
536 answer->psz_status = strdup( "Unknown session id" );
538 answer->p_body = NULL;
544 /* TODO: do something useful */
546 answer->i_status = 200;
547 answer->psz_status = strdup( "OK" );
549 answer->p_body = NULL;
551 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
552 i_port, i_port + 1 );
554 else /* TODO strstr( psz_transport, "interleaved" ) ) */
556 answer->i_status = 400;
557 answer->psz_status = strdup( "Bad Request" );
559 answer->p_body = NULL;
568 httpd_MsgAdd( answer, "Server", "VLC Server" );
569 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
570 httpd_MsgAdd( answer, "Cseq", "%d",
571 atoi( httpd_MsgGet( query, "Cseq" ) ) );
572 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
576 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
582 /*****************************************************************************
584 * FIXME: need to be moved to a common place ?
585 *****************************************************************************/
586 static char *SDPGenerate( vod_media_t *p_media, char *psz_destination )