]> git.sesse.net Git - vlc/blob - modules/misc/rtsp.c
* src/misc/vlm.c: started "vod server" integration (not much done yet).
[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     memset( p_media, 0, sizeof(vod_media_t) );
218
219     asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
220
221     p_media->p_rtsp_url =
222         httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, 0, 0 );
223
224     if( !p_media->p_rtsp_url )
225     {
226         msg_Err( p_vod, "cannot create http url" );
227         free( p_media->psz_rtsp_path );
228         free( p_media );
229         return 0;
230     }
231
232     msg_Dbg( p_vod, "created rtsp url: %s", p_media->psz_rtsp_path );
233
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 );
236
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 );
245
246     p_media->p_vod = p_vod;
247
248     TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
249
250     return p_media;
251 }
252
253 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
254 {
255     vod_sys_t *p_sys = p_vod->p_sys;
256
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 );
261
262     TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
263     free( p_media );
264 }
265
266 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
267 {
268     media_es_t *p_es = malloc( sizeof(media_es_t) );
269     memset( p_es, 0, sizeof(media_es_t) );
270
271     TAB_APPEND( p_media->i_es, p_media->es, p_es );
272
273     /* TODO: update SDP, etc... */
274
275     if( p_media->p_rtsp_url )
276     {
277         char psz_urlc[strlen( p_media->psz_rtsp_control ) + 1 + 10];
278
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 );
282
283         p_es->p_rtsp_url =
284             httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, 0, 0 );
285
286         if( p_es->p_rtsp_url )
287         {
288             httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
289                             RtspCallbackId, (void*)p_es );
290 #if 0
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 );
295 #endif
296         }
297     }
298
299     p_es->p_vod = p_vod;
300     p_es->p_media = p_media;
301
302     return VLC_SUCCESS;
303 }
304
305 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
306 {
307     media_es_t *p_es = 0;
308
309     TAB_REMOVE( p_media->i_es, p_media->es, p_es );
310
311     /* TODO do something useful */
312
313     if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
314 }
315
316 /****************************************************************************
317  * RTSP server implementation
318  ****************************************************************************/
319 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
320 {
321     rtsp_client_t *rtsp = malloc( sizeof(rtsp_client_t) );
322
323     rtsp->psz_session = psz_session;
324     rtsp->i_last = 0;
325     rtsp->b_playing = VLC_FALSE;
326
327     TAB_APPEND( p_media->i_rtsp, p_media->rtsp, rtsp );
328
329     msg_Dbg( p_media->p_vod, "new session: %s", psz_session );
330
331     return rtsp;
332 }
333
334 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
335 {
336     int i;
337
338     for( i = 0; i < p_media->i_rtsp; i++ )
339     {
340         if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
341         {
342             return p_media->rtsp[i];
343         }
344     }
345
346     return NULL;
347 }
348
349 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *rtsp )
350 {
351     msg_Dbg( p_media->p_vod, "closing session: %s", rtsp->psz_session );
352
353     TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, rtsp );
354
355     free( rtsp->psz_session );
356     free( rtsp );
357 }
358
359 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
360                          httpd_message_t *answer, httpd_message_t *query )
361 {
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;
366
367     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
368
369     fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
370
371     answer->i_proto   = HTTPD_PROTO_RTSP;
372     answer->i_version = query->i_version;
373     answer->i_type    = HTTPD_MSG_ANSWER;
374
375     switch( query->i_type )
376     {
377         case HTTPD_MSG_DESCRIBE:
378         {
379             char *psz_sdp =
380                 SDPGenerate( p_media, psz_destination ?
381                              psz_destination : "0.0.0.0" );
382
383             answer->i_status = 200;
384             answer->psz_status = strdup( "OK" );
385             httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );
386
387             answer->p_body = psz_sdp;
388             answer->i_body = strlen( psz_sdp );
389             break;
390         }
391
392         case HTTPD_MSG_PLAY:
393         {
394             rtsp_client_t *rtsp;
395
396             /* for now only multicast so easy */
397             answer->i_status = 200;
398             answer->psz_status = strdup( "OK" );
399             answer->i_body = 0;
400             answer->p_body = NULL;
401
402             psz_session = httpd_MsgGet( query, "Session" );
403             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
404
405             rtsp = RtspClientGet( p_media, psz_session );
406             if( rtsp && !rtsp->b_playing )
407             {
408                 rtsp->b_playing = VLC_TRUE;
409                 /* TODO: do something useful */
410             }
411             break;
412         }
413
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 */
418             return VLC_EGENERIC;
419
420         case HTTPD_MSG_TEARDOWN:
421         {
422             rtsp_client_t *rtsp;
423
424             /* for now only multicast so easy again */
425             answer->i_status = 200;
426             answer->psz_status = strdup( "OK" );
427             answer->i_body = 0;
428             answer->p_body = NULL;
429
430             psz_session = httpd_MsgGet( query, "Session" );
431             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
432
433             rtsp = RtspClientGet( p_media, psz_session );
434             if( rtsp )
435             {
436                 /* TODO: do something useful */
437                 RtspClientDel( p_media, rtsp );
438             }
439             break;
440         }
441
442         default:
443             return VLC_EGENERIC;
444     }
445
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" );
451
452     if( psz_session )
453     {
454         httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
455     }
456
457     return VLC_SUCCESS;
458 }
459
460 static int RtspCallbackId( httpd_callback_sys_t *p_args, httpd_client_t *cl,
461                            httpd_message_t *answer, httpd_message_t *query )
462 {
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;
467
468     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
469
470     fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
471
472     answer->i_proto   = HTTPD_PROTO_RTSP;
473     answer->i_version = query->i_version;
474     answer->i_type    = HTTPD_MSG_ANSWER;
475
476     switch( query->i_type )
477     {
478     case HTTPD_MSG_SETUP:
479         psz_transport = httpd_MsgGet( query, "Transport" );
480         fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
481
482         if( strstr( psz_transport, "multicast" ) && p_media->psz_destination )
483         {
484             fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
485             answer->i_status = 200;
486             answer->psz_status = strdup( "OK" );
487             answer->i_body = 0;
488             answer->p_body = NULL;
489
490             psz_session = httpd_MsgGet( query, "Session" );
491             if( !psz_session || !*psz_session )
492             {
493                 if( psz_session ) free( psz_session );
494                 asprintf( &psz_session, "%d", rand() );
495             }
496
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 );
501         }
502         else if( strstr( psz_transport, "unicast" ) &&
503                  strstr( psz_transport, "client_port=" ) )
504         {
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=") );
509
510             if( !ip )
511             {
512                 answer->i_status = 400;
513                 answer->psz_status = strdup( "Internal server error" );
514                 answer->i_body = 0;
515                 answer->p_body = NULL;
516                 break;
517             }
518
519             fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
520                      ip, i_port );
521
522             psz_session = httpd_MsgGet( query, "Session" );
523             if( !psz_session || !*psz_session )
524             {
525                 if( psz_session ) free( psz_session );
526                 asprintf( &psz_session, "%d", rand() );
527                 rtsp = RtspClientNew( p_media, psz_session );
528             }
529             else
530             {
531                 rtsp = RtspClientGet( p_media, psz_session );
532                 if( !rtsp )
533                 {
534                     /* FIXME right error code */
535                     answer->i_status = 400;
536                     answer->psz_status = strdup( "Unknown session id" );
537                     answer->i_body = 0;
538                     answer->p_body = NULL;
539                     free( ip );
540                     break;
541                 }
542             }
543
544             /* TODO: do something useful */
545
546             answer->i_status = 200;
547             answer->psz_status = strdup( "OK" );
548             answer->i_body = 0;
549             answer->p_body = NULL;
550
551             httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
552                           i_port, i_port + 1 );
553         }
554         else /* TODO  strstr( psz_transport, "interleaved" ) ) */
555         {
556             answer->i_status = 400;
557             answer->psz_status = strdup( "Bad Request" );
558             answer->i_body = 0;
559             answer->p_body = NULL;
560         }
561         break;
562
563         default:
564             return VLC_EGENERIC;
565             break;
566     }
567
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" );
573
574     if( psz_session )
575     {
576         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
577     }
578
579     return VLC_SUCCESS;
580 }
581
582 /*****************************************************************************
583  * SDPGenerate: TODO
584  * FIXME: need to be moved to a common place ?
585  *****************************************************************************/
586 static char *SDPGenerate( vod_media_t *p_media, char *psz_destination )
587 {
588     return strdup( "" );
589 }