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) );
219 memset( p_media, 0, sizeof(vod_media_t) );
220 asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
222 p_media->p_rtsp_url =
223 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, 0, 0 );
225 if( !p_media->p_rtsp_url )
227 msg_Err( p_vod, "cannot create http url" );
228 free( p_media->psz_rtsp_path );
233 msg_Dbg( p_vod, "created rtsp url: %s", p_media->psz_rtsp_path );
235 asprintf( &p_media->psz_rtsp_control, "rtsp://%s:%d%s",
236 p_sys->psz_host, p_sys->i_port, p_media->psz_rtsp_path );
238 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
239 RtspCallback, (void*)p_media );
240 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
241 RtspCallback, (void*)p_media );
242 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
243 RtspCallback, (void*)p_media );
244 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
245 RtspCallback, (void*)p_media );
247 p_media->p_vod = p_vod;
249 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
251 vlc_mutex_lock( &p_item->lock );
252 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
253 for( i = 0; i < p_item->i_es; i++ )
254 msg_Dbg( p_vod, " - ES %i: %4.4s", i, (char *)&p_item->es[i]->i_codec );
255 vlc_mutex_unlock( &p_item->lock );
260 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
262 vod_sys_t *p_sys = p_vod->p_sys;
264 while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
265 httpd_UrlDelete( p_media->p_rtsp_url );
266 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
267 if( p_media->psz_rtsp_control ) free( p_media->psz_rtsp_control );
269 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
273 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
275 media_es_t *p_es = malloc( sizeof(media_es_t) );
276 memset( p_es, 0, sizeof(media_es_t) );
278 TAB_APPEND( p_media->i_es, p_media->es, p_es );
280 /* TODO: update SDP, etc... */
282 if( p_media->p_rtsp_url )
284 char psz_urlc[strlen( p_media->psz_rtsp_control ) + 1 + 10];
286 sprintf( psz_urlc, "%s/trackid=%d",
287 p_media->psz_rtsp_path, p_media->i_es );
288 fprintf( stderr, "rtsp: adding %s\n", psz_urlc );
291 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, 0, 0 );
293 if( p_es->p_rtsp_url )
295 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
296 RtspCallbackId, (void*)p_es );
298 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
299 RtspCallback, (void*)p_es );
300 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
301 RtspCallback, (void*)p_es );
307 p_es->p_media = p_media;
312 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
314 media_es_t *p_es = 0;
316 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
318 /* TODO do something useful */
320 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
323 /****************************************************************************
324 * RTSP server implementation
325 ****************************************************************************/
326 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
328 rtsp_client_t *rtsp = malloc( sizeof(rtsp_client_t) );
330 rtsp->psz_session = psz_session;
332 rtsp->b_playing = VLC_FALSE;
334 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, rtsp );
336 msg_Dbg( p_media->p_vod, "new session: %s", psz_session );
341 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
345 for( i = 0; i < p_media->i_rtsp; i++ )
347 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
349 return p_media->rtsp[i];
356 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *rtsp )
358 msg_Dbg( p_media->p_vod, "closing session: %s", rtsp->psz_session );
360 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, rtsp );
362 free( rtsp->psz_session );
366 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
367 httpd_message_t *answer, httpd_message_t *query )
369 vod_media_t *p_media = (vod_media_t*)p_args;
370 vod_t *p_vod = p_media->p_vod;
371 char *psz_destination = p_media->psz_destination;
372 char *psz_session = NULL;
374 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
376 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
378 answer->i_proto = HTTPD_PROTO_RTSP;
379 answer->i_version = query->i_version;
380 answer->i_type = HTTPD_MSG_ANSWER;
382 switch( query->i_type )
384 case HTTPD_MSG_DESCRIBE:
387 SDPGenerate( p_media, psz_destination ?
388 psz_destination : "0.0.0.0" );
390 answer->i_status = 200;
391 answer->psz_status = strdup( "OK" );
392 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
394 answer->p_body = psz_sdp;
395 answer->i_body = strlen( psz_sdp );
403 /* for now only multicast so easy */
404 answer->i_status = 200;
405 answer->psz_status = strdup( "OK" );
407 answer->p_body = NULL;
409 psz_session = httpd_MsgGet( query, "Session" );
410 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
412 rtsp = RtspClientGet( p_media, psz_session );
413 if( rtsp && !rtsp->b_playing )
415 rtsp->b_playing = VLC_TRUE;
416 /* TODO: do something useful */
421 case HTTPD_MSG_PAUSE:
422 psz_session = httpd_MsgGet( query, "Session" );
423 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
424 /* TODO: do something useful */
427 case HTTPD_MSG_TEARDOWN:
431 /* for now only multicast so easy again */
432 answer->i_status = 200;
433 answer->psz_status = strdup( "OK" );
435 answer->p_body = NULL;
437 psz_session = httpd_MsgGet( query, "Session" );
438 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
440 rtsp = RtspClientGet( p_media, psz_session );
443 /* TODO: do something useful */
444 RtspClientDel( p_media, rtsp );
453 httpd_MsgAdd( answer, "Server", "VLC Server" );
454 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
455 httpd_MsgAdd( answer, "Cseq", "%d",
456 atoi( httpd_MsgGet( query, "Cseq" ) ) );
457 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
461 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
467 static int RtspCallbackId( httpd_callback_sys_t *p_args, httpd_client_t *cl,
468 httpd_message_t *answer, httpd_message_t *query )
470 vod_media_t *p_media = (vod_media_t*)p_args;
471 vod_t *p_vod = p_media->p_vod;
472 char *psz_session = NULL;
473 char *psz_transport = NULL;
475 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
477 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
479 answer->i_proto = HTTPD_PROTO_RTSP;
480 answer->i_version = query->i_version;
481 answer->i_type = HTTPD_MSG_ANSWER;
483 switch( query->i_type )
485 case HTTPD_MSG_SETUP:
486 psz_transport = httpd_MsgGet( query, "Transport" );
487 fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
489 if( strstr( psz_transport, "multicast" ) && p_media->psz_destination )
491 fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
492 answer->i_status = 200;
493 answer->psz_status = strdup( "OK" );
495 answer->p_body = NULL;
497 psz_session = httpd_MsgGet( query, "Session" );
498 if( !psz_session || !*psz_session )
500 if( psz_session ) free( psz_session );
501 asprintf( &psz_session, "%d", rand() );
504 httpd_MsgAdd( answer, "Transport",
505 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
506 p_media->psz_destination, p_media->i_port,
507 p_media->i_port+1, p_media->i_ttl );
509 else if( strstr( psz_transport, "unicast" ) &&
510 strstr( psz_transport, "client_port=" ) )
512 rtsp_client_t *rtsp = NULL;
513 char *ip = httpd_ClientIP( cl );
514 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
515 strlen("client_port=") );
519 answer->i_status = 400;
520 answer->psz_status = strdup( "Internal server error" );
522 answer->p_body = NULL;
526 fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
529 psz_session = httpd_MsgGet( query, "Session" );
530 if( !psz_session || !*psz_session )
532 if( psz_session ) free( psz_session );
533 asprintf( &psz_session, "%d", rand() );
534 rtsp = RtspClientNew( p_media, psz_session );
538 rtsp = RtspClientGet( p_media, psz_session );
541 /* FIXME right error code */
542 answer->i_status = 400;
543 answer->psz_status = strdup( "Unknown session id" );
545 answer->p_body = NULL;
551 /* TODO: do something useful */
553 answer->i_status = 200;
554 answer->psz_status = strdup( "OK" );
556 answer->p_body = NULL;
558 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
559 i_port, i_port + 1 );
561 else /* TODO strstr( psz_transport, "interleaved" ) ) */
563 answer->i_status = 400;
564 answer->psz_status = strdup( "Bad Request" );
566 answer->p_body = NULL;
575 httpd_MsgAdd( answer, "Server", "VLC Server" );
576 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
577 httpd_MsgAdd( answer, "Cseq", "%d",
578 atoi( httpd_MsgGet( query, "Cseq" ) ) );
579 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
583 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
589 /*****************************************************************************
591 * FIXME: need to be moved to a common place ?
592 *****************************************************************************/
593 static char *SDPGenerate( vod_media_t *p_media, char *psz_destination )