]> git.sesse.net Git - vlc/blob - modules/misc/rtsp.c
* modules/stream_out/description.c: new "description" stream output module which...
[vlc] / modules / misc / rtsp.c
1 /*****************************************************************************
2  * rtsp.c: rtsp VoD server module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <errno.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/input.h>
34 #include <vlc/sout.h>
35
36 #include "vlc_httpd.h"
37 #include "vlc_vod.h"
38 #include "network.h"
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
45
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
50 vlc_module_begin();
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 );
56 vlc_module_end();
57
58 /*****************************************************************************
59  * Exported prototypes
60  *****************************************************************************/
61
62 typedef struct
63 {
64     char *psz_session;
65     int64_t i_last; /* for timeout */
66
67     vlc_bool_t b_playing; /* is it in "play" state */
68
69 } rtsp_client_t;
70
71 typedef struct
72 {
73     /* VoD server */
74     vod_t *p_vod;
75
76     /* RTSP server */
77     httpd_url_t  *p_rtsp_url;
78
79     vod_media_t *p_media;
80
81 } media_es_t;
82
83 struct vod_media_t
84 {
85     /* VoD server */
86     vod_t *p_vod;
87
88     /* RTSP server */
89     httpd_url_t  *p_rtsp_url;
90     char         *psz_rtsp_control;
91     char         *psz_rtsp_path;
92
93     char *psz_destination;
94     int  i_port;
95     int  i_port_audio;
96     int  i_port_video;
97     int  i_ttl;
98
99     /* ES list */
100     int        i_es;
101     media_es_t **es;
102
103     /* RTSP client */
104     int           i_rtsp;
105     rtsp_client_t **rtsp;
106 };
107
108 struct vod_sys_t
109 {
110     /* RTSP server */
111     httpd_host_t *p_rtsp_host;
112     char *psz_host;
113     char *psz_path;
114     int i_port;
115
116     /* List of media */
117     int i_media;
118     vod_media_t **media;
119 };
120
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 * );
125
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 * );
129
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 * );
134
135 static char *SDPGenerate( vod_media_t *, char * );
136
137 /*****************************************************************************
138  * Open: Starts the RTSP server module
139  *****************************************************************************/
140 static int Open( vlc_object_t *p_this )
141 {
142     vod_t *p_vod = (vod_t *)p_this;
143     vod_sys_t *p_sys = 0;
144     char *psz_url = 0;
145     vlc_url_t url;
146
147     psz_url = config_GetPsz( p_vod, "rtsp-host" );
148     vlc_UrlParse( &url, psz_url, 0 );
149     if( psz_url ) free( psz_url );
150
151     if( !url.psz_host || !*url.psz_host )
152     {
153         if( url.psz_host ) free( url.psz_host );
154         url.psz_host = strdup( "localhost" );
155     }
156     if( url.i_port <= 0 ) url.i_port = 554;
157
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;
161
162     p_sys->p_rtsp_host =
163         httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
164     if( !p_sys->p_rtsp_host )
165     {
166         msg_Err( p_vod, "cannot create http server (%s:%i)",
167                  url.psz_host, url.i_port );
168         goto error;
169     }
170
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;
174
175     vlc_UrlClean( &url );
176     p_sys->media = 0;
177     p_sys->i_media = 0;
178
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;
183
184     return VLC_SUCCESS;
185
186  error:
187
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 );
191     return VLC_EGENERIC;
192 }
193
194 /*****************************************************************************
195  * Close:
196  *****************************************************************************/
197 static void Close( vlc_object_t * p_this )
198 {
199     vod_t *p_vod = (vod_t *)p_this;
200     vod_sys_t *p_sys = p_vod->p_sys;
201
202     httpd_HostDelete( p_sys->p_rtsp_host );
203
204     /* TODO delete medias */
205
206     free( p_sys );
207 }
208
209 /*****************************************************************************
210  * Media handling
211  *****************************************************************************/
212 static vod_media_t *MediaNew( vod_t *p_vod, char *psz_name,
213                               input_item_t *p_item )
214 {
215     vod_sys_t *p_sys = p_vod->p_sys;
216     vod_media_t *p_media = malloc( sizeof(vod_media_t) );
217     int i;
218
219     memset( p_media, 0, sizeof(vod_media_t) );
220     asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
221
222     p_media->p_rtsp_url =
223         httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, 0, 0 );
224
225     if( !p_media->p_rtsp_url )
226     {
227         msg_Err( p_vod, "cannot create http url" );
228         free( p_media->psz_rtsp_path );
229         free( p_media );
230         return 0;
231     }
232
233     msg_Dbg( p_vod, "created rtsp url: %s", p_media->psz_rtsp_path );
234
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 );
237
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 );
246
247     p_media->p_vod = p_vod;
248
249     TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
250
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 );
256
257     return p_media;
258 }
259
260 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
261 {
262     vod_sys_t *p_sys = p_vod->p_sys;
263
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 );
268
269     TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
270     free( p_media );
271 }
272
273 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
274 {
275     media_es_t *p_es = malloc( sizeof(media_es_t) );
276     memset( p_es, 0, sizeof(media_es_t) );
277
278     TAB_APPEND( p_media->i_es, p_media->es, p_es );
279
280     /* TODO: update SDP, etc... */
281
282     if( p_media->p_rtsp_url )
283     {
284         char psz_urlc[strlen( p_media->psz_rtsp_control ) + 1 + 10];
285
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 );
289
290         p_es->p_rtsp_url =
291             httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, 0, 0 );
292
293         if( p_es->p_rtsp_url )
294         {
295             httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
296                             RtspCallbackId, (void*)p_es );
297 #if 0
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 );
302 #endif
303         }
304     }
305
306     p_es->p_vod = p_vod;
307     p_es->p_media = p_media;
308
309     return VLC_SUCCESS;
310 }
311
312 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
313 {
314     media_es_t *p_es = 0;
315
316     TAB_REMOVE( p_media->i_es, p_media->es, p_es );
317
318     /* TODO do something useful */
319
320     if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
321 }
322
323 /****************************************************************************
324  * RTSP server implementation
325  ****************************************************************************/
326 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
327 {
328     rtsp_client_t *rtsp = malloc( sizeof(rtsp_client_t) );
329
330     rtsp->psz_session = psz_session;
331     rtsp->i_last = 0;
332     rtsp->b_playing = VLC_FALSE;
333
334     TAB_APPEND( p_media->i_rtsp, p_media->rtsp, rtsp );
335
336     msg_Dbg( p_media->p_vod, "new session: %s", psz_session );
337
338     return rtsp;
339 }
340
341 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
342 {
343     int i;
344
345     for( i = 0; i < p_media->i_rtsp; i++ )
346     {
347         if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
348         {
349             return p_media->rtsp[i];
350         }
351     }
352
353     return NULL;
354 }
355
356 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *rtsp )
357 {
358     msg_Dbg( p_media->p_vod, "closing session: %s", rtsp->psz_session );
359
360     TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, rtsp );
361
362     free( rtsp->psz_session );
363     free( rtsp );
364 }
365
366 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
367                          httpd_message_t *answer, httpd_message_t *query )
368 {
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;
373
374     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
375
376     fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
377
378     answer->i_proto   = HTTPD_PROTO_RTSP;
379     answer->i_version = query->i_version;
380     answer->i_type    = HTTPD_MSG_ANSWER;
381
382     switch( query->i_type )
383     {
384         case HTTPD_MSG_DESCRIBE:
385         {
386             char *psz_sdp =
387                 SDPGenerate( p_media, psz_destination ?
388                              psz_destination : "0.0.0.0" );
389
390             answer->i_status = 200;
391             answer->psz_status = strdup( "OK" );
392             httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );
393
394             answer->p_body = psz_sdp;
395             answer->i_body = strlen( psz_sdp );
396             break;
397         }
398
399         case HTTPD_MSG_PLAY:
400         {
401             rtsp_client_t *rtsp;
402
403             /* for now only multicast so easy */
404             answer->i_status = 200;
405             answer->psz_status = strdup( "OK" );
406             answer->i_body = 0;
407             answer->p_body = NULL;
408
409             psz_session = httpd_MsgGet( query, "Session" );
410             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
411
412             rtsp = RtspClientGet( p_media, psz_session );
413             if( rtsp && !rtsp->b_playing )
414             {
415                 rtsp->b_playing = VLC_TRUE;
416                 /* TODO: do something useful */
417             }
418             break;
419         }
420
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 */
425             return VLC_EGENERIC;
426
427         case HTTPD_MSG_TEARDOWN:
428         {
429             rtsp_client_t *rtsp;
430
431             /* for now only multicast so easy again */
432             answer->i_status = 200;
433             answer->psz_status = strdup( "OK" );
434             answer->i_body = 0;
435             answer->p_body = NULL;
436
437             psz_session = httpd_MsgGet( query, "Session" );
438             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
439
440             rtsp = RtspClientGet( p_media, psz_session );
441             if( rtsp )
442             {
443                 /* TODO: do something useful */
444                 RtspClientDel( p_media, rtsp );
445             }
446             break;
447         }
448
449         default:
450             return VLC_EGENERIC;
451     }
452
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" );
458
459     if( psz_session )
460     {
461         httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
462     }
463
464     return VLC_SUCCESS;
465 }
466
467 static int RtspCallbackId( httpd_callback_sys_t *p_args, httpd_client_t *cl,
468                            httpd_message_t *answer, httpd_message_t *query )
469 {
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;
474
475     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
476
477     fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
478
479     answer->i_proto   = HTTPD_PROTO_RTSP;
480     answer->i_version = query->i_version;
481     answer->i_type    = HTTPD_MSG_ANSWER;
482
483     switch( query->i_type )
484     {
485     case HTTPD_MSG_SETUP:
486         psz_transport = httpd_MsgGet( query, "Transport" );
487         fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
488
489         if( strstr( psz_transport, "multicast" ) && p_media->psz_destination )
490         {
491             fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
492             answer->i_status = 200;
493             answer->psz_status = strdup( "OK" );
494             answer->i_body = 0;
495             answer->p_body = NULL;
496
497             psz_session = httpd_MsgGet( query, "Session" );
498             if( !psz_session || !*psz_session )
499             {
500                 if( psz_session ) free( psz_session );
501                 asprintf( &psz_session, "%d", rand() );
502             }
503
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 );
508         }
509         else if( strstr( psz_transport, "unicast" ) &&
510                  strstr( psz_transport, "client_port=" ) )
511         {
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=") );
516
517             if( !ip )
518             {
519                 answer->i_status = 400;
520                 answer->psz_status = strdup( "Internal server error" );
521                 answer->i_body = 0;
522                 answer->p_body = NULL;
523                 break;
524             }
525
526             fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
527                      ip, i_port );
528
529             psz_session = httpd_MsgGet( query, "Session" );
530             if( !psz_session || !*psz_session )
531             {
532                 if( psz_session ) free( psz_session );
533                 asprintf( &psz_session, "%d", rand() );
534                 rtsp = RtspClientNew( p_media, psz_session );
535             }
536             else
537             {
538                 rtsp = RtspClientGet( p_media, psz_session );
539                 if( !rtsp )
540                 {
541                     /* FIXME right error code */
542                     answer->i_status = 400;
543                     answer->psz_status = strdup( "Unknown session id" );
544                     answer->i_body = 0;
545                     answer->p_body = NULL;
546                     free( ip );
547                     break;
548                 }
549             }
550
551             /* TODO: do something useful */
552
553             answer->i_status = 200;
554             answer->psz_status = strdup( "OK" );
555             answer->i_body = 0;
556             answer->p_body = NULL;
557
558             httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
559                           i_port, i_port + 1 );
560         }
561         else /* TODO  strstr( psz_transport, "interleaved" ) ) */
562         {
563             answer->i_status = 400;
564             answer->psz_status = strdup( "Bad Request" );
565             answer->i_body = 0;
566             answer->p_body = NULL;
567         }
568         break;
569
570         default:
571             return VLC_EGENERIC;
572             break;
573     }
574
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" );
580
581     if( psz_session )
582     {
583         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
584     }
585
586     return VLC_SUCCESS;
587 }
588
589 /*****************************************************************************
590  * SDPGenerate: TODO
591  * FIXME: need to be moved to a common place ?
592  *****************************************************************************/
593 static char *SDPGenerate( vod_media_t *p_media, char *psz_destination )
594 {
595     return strdup( "" );
596 }