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