Clarify live555 version error
[vlc/asuraparaju-public.git] / modules / misc / rtsp.c
bloba8ebc4310166c7b510d70feabcfc664527119530
1 /*****************************************************************************
2 * rtsp.c: rtsp VoD server module
3 *****************************************************************************
4 * Copyright (C) 2003-2006 the VideoLAN team
5 * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_sout.h>
37 #include <vlc_block.h>
39 #include <vlc_httpd.h>
40 #include <vlc_vod.h>
41 #include <vlc_url.h>
42 #include <vlc_network.h>
43 #include <vlc_charset.h>
44 #include <vlc_strings.h>
45 #include <vlc_rand.h>
47 #ifndef WIN32
48 # include <locale.h>
49 #endif
51 #ifdef HAVE_XLOCALE_H
52 # include <xlocale.h>
53 #endif
55 /*****************************************************************************
56 * Module descriptor
57 *****************************************************************************/
58 static int Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
61 #define HOST_TEXT N_( "RTSP host address" )
62 #define HOST_LONGTEXT N_( \
63 "This defines the address, port and path the RTSP VOD server will listen " \
64 "on.\nSyntax is address:port/path. The default is to listen on all "\
65 "interfaces (address 0.0.0.0), on port 554, with no path.\nTo listen " \
66 "only on the local interface, use \"localhost\" as address." )
68 #define THROTTLE_TEXT N_( "Maximum number of connections" )
69 #define THROTTLE_LONGTEXT N_( "This limits the maximum number of clients " \
70 "that can connect to the RTSP VOD. 0 means no limit." )
72 #define RAWMUX_TEXT N_( "MUX for RAW RTSP transport" )
74 #define SESSION_TIMEOUT_TEXT N_( "Sets the timeout option in the RTSP " \
75 "session string" )
76 #define SESSION_TIMEOUT_LONGTEXT N_( "Defines what timeout option to add " \
77 "to the RTSP session ID string. Setting it to a negative number removes " \
78 "the timeout option entirely. This is needed by some IPTV STBs (such as " \
79 "those made by HansunTech) which get confused by it. The default is 5." )
81 vlc_module_begin ()
82 set_shortname( N_("RTSP VoD" ) )
83 set_description( N_("RTSP VoD server") )
84 set_category( CAT_SOUT )
85 set_subcategory( SUBCAT_SOUT_VOD )
86 set_capability( "vod server", 1 )
87 set_callbacks( Open, Close )
88 add_shortcut( "rtsp" )
89 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, true )
90 add_string( "rtsp-raw-mux", "ts", NULL, RAWMUX_TEXT,
91 RAWMUX_TEXT, true )
92 add_integer( "rtsp-throttle-users", 0, NULL, THROTTLE_TEXT,
93 THROTTLE_LONGTEXT, true )
94 add_integer( "rtsp-session-timeout", 5, NULL, SESSION_TIMEOUT_TEXT,
95 SESSION_TIMEOUT_LONGTEXT, true )
96 vlc_module_end ()
98 /*****************************************************************************
99 * Exported prototypes
100 *****************************************************************************/
102 typedef struct media_es_t media_es_t;
104 typedef struct
106 media_es_t *p_media_es;
107 int i_port;
109 } rtsp_client_es_t;
111 typedef struct
113 char *psz_session;
115 bool b_playing; /* is it in "play" state */
116 bool b_paused; /* is it in "pause" state */
118 int i_es;
119 rtsp_client_es_t **es;
121 } rtsp_client_t;
123 struct media_es_t
125 /* VoD server */
126 vod_t *p_vod;
128 /* RTSP server */
129 httpd_url_t *p_rtsp_url;
131 vod_media_t *p_media;
133 es_format_t fmt;
134 uint8_t i_payload_type;
135 const char *psz_ptname;
136 unsigned i_clock_rate;
137 unsigned i_channels;
138 char *psz_fmtp;
142 struct vod_media_t
144 int id;
146 /* VoD server */
147 vod_t *p_vod;
149 /* RTSP server */
150 httpd_url_t *p_rtsp_url;
151 char *psz_rtsp_control_v4;
152 char *psz_rtsp_control_v6;
153 char *psz_rtsp_path;
155 int i_payload_type;
157 vlc_mutex_t lock;
159 /* ES list */
160 int i_es;
161 media_es_t **es;
162 const char *psz_mux;
163 bool b_raw;
165 /* RTSP client */
166 int i_rtsp;
167 rtsp_client_t **rtsp;
169 /* Infos */
170 mtime_t i_length;
173 struct vod_sys_t
175 /* RTSP server */
176 httpd_host_t *p_rtsp_host;
177 char *psz_path;
178 int i_port;
179 int i_throttle_users;
180 int i_connections;
182 char *psz_raw_mux;
184 int i_session_timeout;
186 /* List of media */
187 int i_media_id;
188 int i_media;
189 vod_media_t **media;
191 /* */
192 block_fifo_t *p_fifo_cmd;
195 /* rtsp delayed command (to avoid deadlock between vlm/httpd) */
196 typedef enum
198 RTSP_CMD_TYPE_NONE, /* Exit requested */
200 RTSP_CMD_TYPE_PLAY,
201 RTSP_CMD_TYPE_PAUSE,
202 RTSP_CMD_TYPE_STOP,
203 RTSP_CMD_TYPE_SEEK,
204 RTSP_CMD_TYPE_REWIND,
205 RTSP_CMD_TYPE_FORWARD,
207 RTSP_CMD_TYPE_ADD,
208 RTSP_CMD_TYPE_DEL,
209 } rtsp_cmd_type_t;
211 /* */
212 typedef struct
214 int i_type;
215 int i_media_id;
216 vod_media_t *p_media;
217 char *psz_session;
218 char *psz_arg;
219 double f_arg;
220 } rtsp_cmd_t;
222 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
223 static void MediaDel( vod_t *, vod_media_t * );
224 static void MediaAskDel ( vod_t *, vod_media_t * );
225 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
226 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
228 static void* CommandThread( vlc_object_t *p_this );
229 static void CommandPush( vod_t *, rtsp_cmd_type_t, vod_media_t *, const char *psz_session,
230 double f_arg, const char *psz_arg );
232 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
233 static rtsp_client_t *RtspClientGet( vod_media_t *, const char * );
234 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
236 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
237 httpd_message_t *, const httpd_message_t * );
238 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
239 httpd_message_t *, const httpd_message_t * );
241 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
243 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
245 static const char hex[16] = "0123456789abcdef";
247 for( int i = 0; i < i_data; i++ )
249 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
250 s[2*i+1] = hex[(p_data[i] )&0xf];
252 s[2*i_data] = '\0';
255 /*****************************************************************************
256 * Open: Starts the RTSP server module
257 *****************************************************************************/
258 static int Open( vlc_object_t *p_this )
260 vod_t *p_vod = (vod_t *)p_this;
261 vod_sys_t *p_sys = NULL;
262 char *psz_url = NULL;
263 vlc_url_t url;
265 psz_url = var_InheritString( p_vod, "rtsp-host" );
266 vlc_UrlParse( &url, psz_url, 0 );
267 free( psz_url );
269 if( url.i_port <= 0 ) url.i_port = 554;
271 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
272 if( !p_sys ) goto error;
273 p_sys->p_rtsp_host = 0;
275 p_sys->i_session_timeout = var_CreateGetInteger( p_this, "rtsp-session-timeout" );
277 p_sys->i_throttle_users = var_CreateGetInteger( p_this, "rtsp-throttle-users" );
278 msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
279 p_sys->i_connections = 0;
281 p_sys->psz_raw_mux = var_CreateGetString( p_this, "rtsp-raw-mux" );
283 p_sys->p_rtsp_host =
284 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
285 if( !p_sys->p_rtsp_host )
287 msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
288 url.psz_host, url.i_port );
289 goto error;
292 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
293 p_sys->i_port = url.i_port;
295 vlc_UrlClean( &url );
297 TAB_INIT( p_sys->i_media, p_sys->media );
298 p_sys->i_media_id = 0;
300 p_vod->pf_media_new = MediaNew;
301 p_vod->pf_media_del = MediaAskDel;
302 p_vod->pf_media_add_es = MediaAddES;
303 p_vod->pf_media_del_es = MediaDelES;
305 p_sys->p_fifo_cmd = block_FifoNew();
306 if( vlc_thread_create( p_vod, "rtsp vod thread", CommandThread,
307 VLC_THREAD_PRIORITY_LOW ) )
309 msg_Err( p_vod, "cannot spawn rtsp vod thread" );
310 block_FifoRelease( p_sys->p_fifo_cmd );
311 free( p_sys->psz_path );
312 goto error;
315 return VLC_SUCCESS;
317 error:
318 if( p_sys )
320 if( p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
321 free( p_sys->psz_raw_mux );
322 free( p_sys );
324 vlc_UrlClean( &url );
326 return VLC_EGENERIC;
329 /*****************************************************************************
330 * Close:
331 *****************************************************************************/
332 static void Close( vlc_object_t * p_this )
334 vod_t *p_vod = (vod_t *)p_this;
335 vod_sys_t *p_sys = p_vod->p_sys;
337 /* Stop command thread */
338 vlc_object_kill( p_vod );
339 CommandPush( p_vod, RTSP_CMD_TYPE_NONE, NULL, NULL, 0.0, NULL );
340 vlc_thread_join( p_vod );
342 while( block_FifoCount( p_sys->p_fifo_cmd ) > 0 )
344 rtsp_cmd_t cmd;
345 block_t *p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
346 memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
347 block_Release( p_block_cmd );
348 if ( cmd.i_type == RTSP_CMD_TYPE_DEL )
349 MediaDel(p_vod, cmd.p_media);
350 free( cmd.psz_session );
351 free( cmd.psz_arg );
353 block_FifoRelease( p_sys->p_fifo_cmd );
355 httpd_HostDelete( p_sys->p_rtsp_host );
356 var_Destroy( p_this, "rtsp-session-timeout" );
357 var_Destroy( p_this, "rtsp-throttle-users" );
358 var_Destroy( p_this, "rtsp-raw-mux" );
360 /* Check VLM is not buggy */
361 if( p_sys->i_media > 0 )
362 msg_Err( p_vod, "rtsp vod leaking %d medias", p_sys->i_media );
363 TAB_CLEAN( p_sys->i_media, p_sys->media );
365 free( p_sys->psz_path );
366 free( p_sys->psz_raw_mux );
367 free( p_sys );
370 /*****************************************************************************
371 * Media handling
372 *****************************************************************************/
373 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
374 input_item_t *p_item )
376 vod_sys_t *p_sys = p_vod->p_sys;
378 vod_media_t *p_media = calloc( 1, sizeof(vod_media_t) );
379 if( !p_media )
380 return NULL;
382 p_media->id = p_sys->i_media_id++;
383 TAB_INIT( p_media->i_es, p_media->es );
384 p_media->psz_mux = NULL;
385 TAB_INIT( p_media->i_rtsp, p_media->rtsp );
386 p_media->b_raw = false;
388 if( asprintf( &p_media->psz_rtsp_path, "%s%s",
389 p_sys->psz_path, psz_name ) <0 )
390 return NULL;
391 p_media->p_rtsp_url =
392 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
393 NULL, NULL );
395 if( !p_media->p_rtsp_url )
397 msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
398 free( p_media->psz_rtsp_path );
399 free( p_media );
400 return NULL;
403 msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
405 if( asprintf( &p_media->psz_rtsp_control_v4,
406 "rtsp://%%s:%d%s/trackID=%%d",
407 p_sys->i_port, p_media->psz_rtsp_path ) < 0 )
409 httpd_UrlDelete( p_media->p_rtsp_url );
410 free( p_media->psz_rtsp_path );
411 free( p_media );
412 return NULL;
414 if( asprintf( &p_media->psz_rtsp_control_v6,
415 "rtsp://[%%s]:%d%s/trackID=%%d",
416 p_sys->i_port, p_media->psz_rtsp_path ) < 0 )
418 httpd_UrlDelete( p_media->p_rtsp_url );
419 free( p_media->psz_rtsp_path );
420 free( p_media );
421 return NULL;
424 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
425 RtspCallback, (void*)p_media );
426 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
427 RtspCallback, (void*)p_media );
428 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
429 RtspCallback, (void*)p_media );
430 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
431 RtspCallback, (void*)p_media );
432 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_GETPARAMETER,
433 RtspCallback, (void*)p_media );
434 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
435 RtspCallback, (void*)p_media );
437 p_media->p_vod = p_vod;
439 vlc_mutex_init( &p_media->lock );
441 p_media->i_payload_type = 96;
443 p_media->i_length = input_item_GetDuration( p_item );
445 vlc_mutex_lock( &p_item->lock );
446 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
447 for( int i = 0; i < p_item->i_es; i++ )
449 MediaAddES( p_vod, p_media, p_item->es[i] );
451 vlc_mutex_unlock( &p_item->lock );
453 CommandPush( p_vod, RTSP_CMD_TYPE_ADD, p_media, NULL, 0.0, NULL );
454 return p_media;
457 static void MediaAskDel ( vod_t *p_vod, vod_media_t *p_media )
459 CommandPush( p_vod, RTSP_CMD_TYPE_DEL, p_media, NULL, 0.0, NULL );
462 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
464 vod_sys_t *p_sys = p_vod->p_sys;
466 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
468 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
470 httpd_UrlDelete( p_media->p_rtsp_url );
472 while( p_media->i_rtsp > 0 )
473 RtspClientDel( p_media, p_media->rtsp[0] );
474 TAB_CLEAN( p_media->i_rtsp, p_media->rtsp );
476 free( p_media->psz_rtsp_path );
477 free( p_media->psz_rtsp_control_v6 );
478 free( p_media->psz_rtsp_control_v4 );
480 while( p_media->i_es )
481 MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
482 TAB_CLEAN( p_media->i_es, p_media->es );
484 vlc_mutex_destroy( &p_media->lock );
486 free( p_media );
489 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
491 char *psz_urlc;
493 if( p_media->i_payload_type >= 128 )
495 msg_Err( p_vod, "too many elementary streams");
496 return VLC_EGENERIC;
499 media_es_t *p_es = calloc( 1, sizeof(media_es_t) );
500 if( !p_es )
501 return VLC_ENOMEM;
503 p_media->psz_mux = NULL;
505 /* TODO: update SDP, etc... */
506 if( asprintf( &psz_urlc, "%s/trackID=%d",
507 p_media->psz_rtsp_path, p_media->i_es ) < 0 )
509 free( p_es );
510 return VLC_ENOMEM;
512 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
514 p_es->i_clock_rate = 90000;
515 p_es->i_channels = 1;
517 switch( p_fmt->i_codec )
519 case VLC_CODEC_S16B:
520 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
522 p_es->i_payload_type = 11;
524 else if( p_fmt->audio.i_channels == 2 &&
525 p_fmt->audio.i_rate == 44100 )
527 p_es->i_payload_type = 10;
529 else
531 p_es->i_payload_type = p_media->i_payload_type++;
533 p_es->psz_ptname = "L16";
534 p_es->i_clock_rate = p_fmt->audio.i_rate;
535 p_es->i_channels = p_fmt->audio.i_channels;
536 break;
537 case VLC_CODEC_U8:
538 p_es->i_payload_type = p_media->i_payload_type++;
539 p_es->psz_ptname = "L8";
540 p_es->i_clock_rate = p_fmt->audio.i_rate;
541 p_es->i_channels = p_fmt->audio.i_channels;
542 break;
543 case VLC_CODEC_MPGA:
544 p_es->i_payload_type = 14;
545 p_es->psz_ptname = "MPA";
546 break;
547 case VLC_CODEC_MPGV:
548 p_es->i_payload_type = 32;
549 p_es->psz_ptname = "MPV";
550 break;
551 case VLC_CODEC_A52:
552 p_es->i_payload_type = p_media->i_payload_type++;
553 p_es->psz_ptname = "ac3";
554 p_es->i_clock_rate = p_fmt->audio.i_rate;
555 break;
556 case VLC_CODEC_H263:
557 p_es->i_payload_type = p_media->i_payload_type++;
558 p_es->psz_ptname = "H263-1998";
559 break;
560 case VLC_CODEC_H264:
561 p_es->i_payload_type = p_media->i_payload_type++;
562 p_es->psz_ptname = "H264";
563 p_es->psz_fmtp = NULL;
564 /* FIXME AAAAAAAAAAAARRRRRRRRGGGG copied from stream_out/rtp.c */
565 if( p_fmt->i_extra > 0 )
567 uint8_t *p_buffer = p_fmt->p_extra;
568 int i_buffer = p_fmt->i_extra;
569 char *p_64_sps = NULL;
570 char *p_64_pps = NULL;
571 char hexa[6+1];
573 while( i_buffer > 4 )
575 int i_offset = 0;
576 int i_size = 0;
578 while( p_buffer[0] != 0 || p_buffer[1] != 0 ||
579 p_buffer[2] != 1 )
581 p_buffer++;
582 i_buffer--;
583 if( i_buffer == 0 ) break;
586 if( i_buffer < 4 || memcmp(p_buffer, "\x00\x00\x01", 3 ) )
588 /* No startcode found.. */
589 break;
591 p_buffer += 3;
592 i_buffer -= 3;
594 const int i_nal_type = p_buffer[0]&0x1f;
596 i_size = i_buffer;
597 for( i_offset = 0; i_offset+2 < i_buffer ; i_offset++)
599 if( !memcmp(p_buffer + i_offset, "\x00\x00\x01", 3 ) )
601 /* we found another startcode */
602 while( i_offset > 0 && 0 == p_buffer[ i_offset - 1 ] )
603 i_offset--;
604 i_size = i_offset;
605 break;
609 if( i_size == 0 )
611 /* No-info found in nal */
612 continue;
615 if( i_nal_type == 7 )
617 free( p_64_sps );
618 p_64_sps = vlc_b64_encode_binary( p_buffer, i_size );
619 /* XXX: nothing ensures that i_size >= 4 ?? */
620 sprintf_hexa( hexa, &p_buffer[1], 3 );
622 else if( i_nal_type == 8 )
624 free( p_64_pps );
625 p_64_pps = vlc_b64_encode_binary( p_buffer, i_size );
627 i_buffer -= i_size;
628 p_buffer += i_size;
630 /* */
631 if( p_64_sps && p_64_pps )
633 if( asprintf( &p_es->psz_fmtp,
634 "packetization-mode=1;profile-level-id=%s;"
635 "sprop-parameter-sets=%s,%s;", hexa, p_64_sps,
636 p_64_pps ) < 0 )
638 free( p_64_sps );
639 free( p_64_pps );
640 free( psz_urlc );
641 free( p_es );
642 return VLC_ENOMEM;
645 free( p_64_sps );
646 free( p_64_pps );
648 if( !p_es->psz_fmtp )
649 p_es->psz_fmtp = strdup( "packetization-mode=1" );
650 break;
651 case VLC_CODEC_MP4V:
652 p_es->i_payload_type = p_media->i_payload_type++;
653 p_es->psz_ptname = "MP4V-ES";
654 if( p_fmt->i_extra > 0 )
656 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
657 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
658 if( asprintf( &p_es->psz_fmtp,
659 "profile-level-id=3; config=%s;", p_hexa ) == -1 )
660 p_es->psz_fmtp = NULL;
661 free( p_hexa );
663 break;
664 case VLC_CODEC_MP4A:
665 p_es->i_payload_type = p_media->i_payload_type++;
666 p_es->psz_ptname = "mpeg4-generic";
667 p_es->i_clock_rate = p_fmt->audio.i_rate;
668 if( p_fmt->i_extra > 0 )
670 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
671 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
672 if( asprintf( &p_es->psz_fmtp,
673 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
674 "config=%s; SizeLength=13;IndexLength=3; "
675 "IndexDeltaLength=3; Profile=1;", p_hexa ) == -1 )
676 p_es->psz_fmtp = NULL;
677 free( p_hexa );
679 break;
680 case VLC_FOURCC( 'm', 'p', '2', 't' ):
681 p_media->psz_mux = "ts";
682 p_es->i_payload_type = 33;
683 p_es->psz_ptname = "MP2T";
684 break;
685 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
686 p_media->psz_mux = "ps";
687 p_es->i_payload_type = p_media->i_payload_type++;
688 p_es->psz_ptname = "MP2P";
689 break;
690 case VLC_CODEC_AMR_NB:
691 p_es->i_payload_type = p_media->i_payload_type++;
692 p_es->psz_ptname = "AMR";
693 p_es->i_clock_rate = 8000;
694 if(p_fmt->audio.i_channels == 2 )
695 p_es->i_channels = 2;
696 p_es->psz_fmtp = strdup( "octet-align=1" );
697 break;
698 case VLC_CODEC_AMR_WB:
699 p_es->i_payload_type = p_media->i_payload_type++;
700 p_es->psz_ptname = "AMR-WB";
701 p_es->i_clock_rate = 16000;
702 if(p_fmt->audio.i_channels == 2 )
703 p_es->i_channels = 2;
704 p_es->psz_fmtp = strdup( "octet-align=1" );
705 break;
707 default:
708 msg_Err( p_vod, "cannot add this stream (unsupported "
709 "codec: %4.4s)", (char*)&p_fmt->i_codec );
710 free( psz_urlc );
711 free( p_es );
712 return VLC_EGENERIC;
715 p_es->p_rtsp_url =
716 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
717 NULL );
719 if( !p_es->p_rtsp_url )
721 msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
722 free( psz_urlc );
723 free( p_es );
724 return VLC_EGENERIC;
726 free( psz_urlc );
728 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
729 RtspCallbackES, (void*)p_es );
730 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
731 RtspCallbackES, (void*)p_es );
732 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
733 RtspCallbackES, (void*)p_es );
734 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
735 RtspCallbackES, (void*)p_es );
737 es_format_Copy( &p_es->fmt, p_fmt );
738 p_es->p_vod = p_vod;
739 p_es->p_media = p_media;
741 vlc_mutex_lock( &p_media->lock );
742 TAB_APPEND( p_media->i_es, p_media->es, p_es );
743 vlc_mutex_unlock( &p_media->lock );
745 return VLC_SUCCESS;
748 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
750 media_es_t *p_es = NULL;
752 /* Find the ES */
753 for( int i = 0; i < p_media->i_es; i++ )
755 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
756 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
757 p_media->es[i]->fmt.i_id == p_fmt->i_id )
759 p_es = p_media->es[i];
762 if( !p_es ) return;
764 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
766 vlc_mutex_lock( &p_media->lock );
767 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
768 vlc_mutex_unlock( &p_media->lock );
770 free( p_es->psz_fmtp );
772 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
773 es_format_Clean( &p_es->fmt );
774 free( p_es );
777 static void CommandPush( vod_t *p_vod, rtsp_cmd_type_t i_type, vod_media_t *p_media, const char *psz_session,
778 double f_arg, const char *psz_arg )
780 rtsp_cmd_t cmd;
781 block_t *p_cmd;
783 memset( &cmd, 0, sizeof(cmd) );
784 cmd.i_type = i_type;
785 cmd.p_media = p_media;
786 if( p_media )
787 cmd.i_media_id = p_media->id;
788 if( psz_session )
789 cmd.psz_session = strdup(psz_session);
790 cmd.f_arg = f_arg;
791 if( psz_arg )
792 cmd.psz_arg = strdup(psz_arg);
794 p_cmd = block_New( p_vod, sizeof(rtsp_cmd_t) );
795 memcpy( p_cmd->p_buffer, &cmd, sizeof(cmd) );
797 block_FifoPut( p_vod->p_sys->p_fifo_cmd, p_cmd );
800 static void* CommandThread( vlc_object_t *p_this )
802 vod_t *p_vod = (vod_t*)p_this;
803 vod_sys_t *p_sys = p_vod->p_sys;
804 int canc = vlc_savecancel ();
806 while( vlc_object_alive (p_vod) )
808 block_t *p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
809 rtsp_cmd_t cmd;
810 vod_media_t *p_media = NULL;
811 int i;
813 if( !p_block_cmd )
814 break;
816 memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
817 block_Release( p_block_cmd );
819 if( cmd.i_type == RTSP_CMD_TYPE_NONE )
820 break;
822 if ( cmd.i_type == RTSP_CMD_TYPE_ADD )
824 TAB_APPEND( p_sys->i_media, p_sys->media, cmd.p_media );
825 goto next;
828 if ( cmd.i_type == RTSP_CMD_TYPE_DEL )
830 MediaDel(p_vod, cmd.p_media);
831 goto next;
834 /* */
835 for( i = 0; i < p_sys->i_media; i++ )
837 if( p_sys->media[i]->id == cmd.i_media_id )
838 break;
840 if( i >= p_sys->i_media )
842 goto next;
844 p_media = p_sys->media[i];
846 switch( cmd.i_type )
848 case RTSP_CMD_TYPE_PLAY:
849 vod_MediaControl( p_vod, p_media, cmd.psz_session,
850 VOD_MEDIA_PLAY, cmd.psz_arg );
851 break;
852 case RTSP_CMD_TYPE_PAUSE:
853 vod_MediaControl( p_vod, p_media, cmd.psz_session,
854 VOD_MEDIA_PAUSE );
855 break;
857 case RTSP_CMD_TYPE_STOP:
858 vod_MediaControl( p_vod, p_media, cmd.psz_session, VOD_MEDIA_STOP );
859 break;
861 case RTSP_CMD_TYPE_SEEK:
862 vod_MediaControl( p_vod, p_media, cmd.psz_session,
863 VOD_MEDIA_SEEK, cmd.f_arg );
864 break;
866 case RTSP_CMD_TYPE_REWIND:
867 vod_MediaControl( p_vod, p_media, cmd.psz_session,
868 VOD_MEDIA_REWIND, cmd.f_arg );
869 break;
871 case RTSP_CMD_TYPE_FORWARD:
872 vod_MediaControl( p_vod, p_media, cmd.psz_session,
873 VOD_MEDIA_FORWARD, cmd.f_arg );
874 break;
876 default:
877 break;
880 next:
881 free( cmd.psz_session );
882 free( cmd.psz_arg );
885 vlc_restorecancel (canc);
886 return NULL;
889 /****************************************************************************
890 * RTSP server implementation
891 ****************************************************************************/
892 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
894 rtsp_client_t *p_rtsp = calloc( 1, sizeof(rtsp_client_t) );
896 if( !p_rtsp )
897 return NULL;
898 p_rtsp->es = 0;
900 p_rtsp->psz_session = psz_session;
901 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
903 p_media->p_vod->p_sys->i_connections++;
904 msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
905 psz_session, p_media->p_vod->p_sys->i_throttle_users );
907 return p_rtsp;
910 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, const char *psz_session )
912 for( int i = 0; psz_session && i < p_media->i_rtsp; i++ )
914 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
915 return p_media->rtsp[i];
918 return NULL;
921 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
923 p_media->p_vod->p_sys->i_connections--;
924 msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
925 p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
927 while( p_rtsp->i_es )
929 p_rtsp->i_es--;
930 free( p_rtsp->es[p_rtsp->i_es] );
932 free( p_rtsp->es );
934 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
936 free( p_rtsp->psz_session );
937 free( p_rtsp );
941 static float ParseNPT (const char *str)
943 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
944 locale_t oldloc = uselocale (loc);
945 unsigned hour, min;
946 float sec;
948 if (sscanf (str, "%u:%u:%f", &hour, &min, &sec) == 3)
949 sec += ((hour * 60) + min) * 60;
950 else
951 if (sscanf (str, "%f", &sec) != 1)
952 sec = 0.;
954 if (loc != (locale_t)0)
956 uselocale (oldloc);
957 freelocale (loc);
959 return sec;
963 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
964 httpd_message_t *answer, const httpd_message_t *query )
966 vod_media_t *p_media = (vod_media_t*)p_args;
967 vod_t *p_vod = p_media->p_vod;
968 const char *psz_transport = NULL;
969 const char *psz_playnow = NULL; /* support option: x-playNow */
970 const char *psz_session = NULL;
971 const char *psz_cseq = NULL;
972 rtsp_client_t *p_rtsp;
973 int i_port = 0;
974 int i_cseq = 0;
976 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
978 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
980 answer->i_proto = HTTPD_PROTO_RTSP;
981 answer->i_version = query->i_version;
982 answer->i_type = HTTPD_MSG_ANSWER;
983 answer->i_body = 0;
984 answer->p_body = NULL;
986 switch( query->i_type )
988 case HTTPD_MSG_SETUP:
990 psz_playnow = httpd_MsgGet( query, "x-playNow" );
991 psz_transport = httpd_MsgGet( query, "Transport" );
992 if( psz_transport == NULL )
994 answer->i_status = 400;
995 break;
997 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
999 if( strstr( psz_transport, "unicast" ) &&
1000 strstr( psz_transport, "client_port=" ) )
1002 rtsp_client_t *p_rtsp = NULL;
1003 char ip[NI_MAXNUMERICHOST];
1004 i_port = atoi( strstr( psz_transport, "client_port=" ) +
1005 strlen("client_port=") );
1007 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
1008 strstr( psz_transport, "RAW/RAW/UDP" ) )
1010 p_media->psz_mux = p_vod->p_sys->psz_raw_mux;
1011 p_media->b_raw = true;
1014 if( httpd_ClientIP( cl, ip ) == NULL )
1016 answer->i_status = 500;
1017 answer->i_body = 0;
1018 answer->p_body = NULL;
1019 break;
1022 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1023 ip, i_port );
1025 psz_session = httpd_MsgGet( query, "Session" );
1026 if( !psz_session || !*psz_session )
1028 char *psz_new;
1029 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1030 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1032 answer->i_status = 503;
1033 answer->i_body = 0;
1034 answer->p_body = NULL;
1035 break;
1037 #warning Should use secure randomness here! (spoofing risk)
1038 if( asprintf( &psz_new, "%lu", vlc_mrand48() ) < 0 )
1039 return VLC_ENOMEM;
1040 psz_session = psz_new;
1042 p_rtsp = RtspClientNew( p_media, psz_new );
1043 if( !p_rtsp )
1045 answer->i_status = 454;
1046 answer->i_body = 0;
1047 answer->p_body = NULL;
1048 break;
1051 else
1053 p_rtsp = RtspClientGet( p_media, psz_session );
1054 if( !p_rtsp )
1056 answer->i_status = 454;
1057 answer->i_body = 0;
1058 answer->p_body = NULL;
1059 break;
1063 answer->i_status = 200;
1064 answer->i_body = 0;
1065 answer->p_body = NULL;
1067 if( p_media->b_raw )
1069 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1071 httpd_MsgAdd( answer, "Transport",
1072 "MP2T/H2221/UDP;unicast;client_port=%d-%d",
1073 i_port, i_port + 1 );
1075 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1077 httpd_MsgAdd( answer, "Transport",
1078 "RAW/RAW/UDP;unicast;client_port=%d-%d",
1079 i_port, i_port + 1 );
1082 else
1083 httpd_MsgAdd( answer, "Transport",
1084 "RTP/AVP/UDP;unicast;client_port=%d-%d",
1085 i_port, i_port + 1 );
1087 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1089 answer->i_status = 461;
1090 answer->i_body = 0;
1091 answer->p_body = NULL;
1094 /* Intentional fall-through on x-playNow option in RTSP request */
1095 if( !psz_playnow )
1096 break;
1099 case HTTPD_MSG_PLAY:
1101 char *psz_output, ip[NI_MAXNUMERICHOST];
1102 int i_port_audio = 0, i_port_video = 0;
1104 /* for now only multicast so easy */
1105 if( !psz_playnow )
1107 answer->i_status = 200;
1108 answer->i_body = 0;
1109 answer->p_body = NULL;
1112 if( !psz_session )
1113 psz_session = httpd_MsgGet( query, "Session" );
1114 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1116 p_rtsp = RtspClientGet( p_media, psz_session );
1117 if( !p_rtsp )
1119 answer->i_status = 500;
1120 answer->i_body = 0;
1121 answer->p_body = NULL;
1122 break;
1125 if( p_rtsp->b_playing )
1127 const char *psz_position = httpd_MsgGet( query, "Range" );
1128 const char *psz_scale = httpd_MsgGet( query, "Scale" );
1129 if( psz_position )
1130 psz_position = strstr( psz_position, "npt=" );
1131 if( psz_position && !psz_scale )
1133 double f_pos = ParseNPT (psz_position + 4);
1134 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1135 f_pos /= ((double)(p_media->i_length)) / CLOCK_FREQ / 100;
1136 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media,
1137 psz_session, f_pos, NULL );
1139 else if( psz_scale )
1141 double f_scale = 0.0;
1142 char *end;
1144 f_scale = us_strtod( psz_scale, &end );
1145 if( end > psz_scale )
1147 f_scale = (f_scale * 30.0);
1148 if( psz_scale[0] == '-' ) /* rewind */
1150 msg_Dbg( p_vod, "rewind request: %s", psz_scale );
1151 CommandPush( p_vod, RTSP_CMD_TYPE_REWIND, p_media,
1152 psz_session, f_scale, NULL );
1154 else if(psz_scale[0] != '1' ) /* fast-forward */
1156 msg_Dbg( p_vod, "fastforward request: %s",
1157 psz_scale );
1158 CommandPush( p_vod, RTSP_CMD_TYPE_FORWARD, p_media,
1159 psz_session, f_scale, NULL );
1162 if( p_rtsp->b_paused )
1164 p_rtsp->b_paused = false;
1165 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media,
1166 psz_session, 0, NULL );
1169 break;
1173 if( p_rtsp->b_playing && p_rtsp->b_paused )
1175 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media,
1176 psz_session, 0, NULL );
1177 p_rtsp->b_paused = false;
1178 break;
1180 else if( p_rtsp->b_playing ) break;
1182 if( httpd_ClientIP( cl, ip ) == NULL ) break;
1184 p_rtsp->b_playing = true;
1186 /* FIXME for != 1 video and 1 audio */
1187 for( int i = 0; i < p_rtsp->i_es; i++ )
1189 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
1190 i_port_audio = p_rtsp->es[i]->i_port;
1191 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
1192 i_port_video = p_rtsp->es[i]->i_port;
1195 if( p_media->psz_mux )
1197 if( p_media->b_raw )
1199 if( asprintf( &psz_output,
1200 "std{access=udp,dst=%s:%i,mux=%s}",
1201 ip, i_port, p_media->psz_mux ) < 0 )
1202 return VLC_ENOMEM;
1204 else
1206 if( asprintf( &psz_output,
1207 "rtp{dst=%s,port=%i,mux=%s}",
1208 ip, i_port_video, p_media->psz_mux ) < 0 )
1209 return VLC_ENOMEM;
1212 else
1214 if( asprintf( &psz_output,
1215 "rtp{dst=%s,port-video=%i,port-audio=%i}",
1216 ip, i_port_video, i_port_audio ) < 0 )
1217 return VLC_ENOMEM;
1220 CommandPush( p_vod, RTSP_CMD_TYPE_PLAY, p_media, psz_session,
1221 0, psz_output );
1222 free( psz_output );
1223 break;
1226 case HTTPD_MSG_DESCRIBE:
1228 char *psz_sdp =
1229 SDPGenerate( p_media, cl );
1231 if( psz_sdp != NULL )
1233 answer->i_status = 200;
1234 httpd_MsgAdd( answer, "Content-type", "%s",
1235 "application/sdp" );
1237 answer->p_body = (uint8_t *)psz_sdp;
1238 answer->i_body = strlen( psz_sdp );
1240 else
1242 answer->i_status = 500;
1243 answer->p_body = NULL;
1244 answer->i_body = 0;
1246 break;
1249 case HTTPD_MSG_PAUSE:
1250 psz_session = httpd_MsgGet( query, "Session" );
1251 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1253 p_rtsp = RtspClientGet( p_media, psz_session );
1254 if( !p_rtsp ) break;
1256 if( !p_rtsp->b_paused )
1258 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session,
1259 0, NULL );
1260 p_rtsp->b_paused = true;
1263 answer->i_status = 200;
1264 answer->i_body = 0;
1265 answer->p_body = NULL;
1266 break;
1268 case HTTPD_MSG_TEARDOWN:
1269 /* for now only multicast so easy again */
1270 answer->i_status = 200;
1271 answer->i_body = 0;
1272 answer->p_body = NULL;
1274 psz_session = httpd_MsgGet( query, "Session" );
1275 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1277 p_rtsp = RtspClientGet( p_media, psz_session );
1278 if( !p_rtsp ) break;
1280 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session,
1281 0, NULL );
1282 RtspClientDel( p_media, p_rtsp );
1283 break;
1285 case HTTPD_MSG_GETPARAMETER:
1286 answer->i_status = 200;
1287 answer->i_body = 0;
1288 answer->p_body = NULL;
1289 break;
1291 default:
1292 return VLC_EGENERIC;
1295 httpd_MsgAdd( answer, "Server", "VLC/%s", VERSION );
1296 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1297 psz_cseq = httpd_MsgGet( query, "Cseq" );
1298 psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
1299 httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
1300 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1302 if( psz_session )
1304 if( p_media->p_vod->p_sys->i_session_timeout >= 0 )
1305 httpd_MsgAdd( answer, "Session", "%s;timeout=%i", psz_session,
1306 p_media->p_vod->p_sys->i_session_timeout );
1307 else
1308 httpd_MsgAdd( answer, "Session", "%s", psz_session );
1311 return VLC_SUCCESS;
1314 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
1315 httpd_message_t *answer,
1316 const httpd_message_t *query )
1318 media_es_t *p_es = (media_es_t*)p_args;
1319 vod_media_t *p_media = p_es->p_media;
1320 vod_t *p_vod = p_media->p_vod;
1321 rtsp_client_t *p_rtsp = NULL;
1322 const char *psz_transport = NULL;
1323 const char *psz_playnow = NULL; /* support option: x-playNow */
1324 const char *psz_session = NULL;
1325 const char *psz_position = NULL;
1326 const char *psz_cseq = NULL;
1327 int i_cseq = 0;
1329 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
1331 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1333 answer->i_proto = HTTPD_PROTO_RTSP;
1334 answer->i_version = query->i_version;
1335 answer->i_type = HTTPD_MSG_ANSWER;
1336 answer->i_body = 0;
1337 answer->p_body = NULL;
1339 switch( query->i_type )
1341 case HTTPD_MSG_SETUP:
1342 psz_playnow = httpd_MsgGet( query, "x-playNow" );
1343 psz_transport = httpd_MsgGet( query, "Transport" );
1345 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1347 if( strstr( psz_transport, "unicast" ) &&
1348 strstr( psz_transport, "client_port=" ) )
1350 rtsp_client_t *p_rtsp = NULL;
1351 rtsp_client_es_t *p_rtsp_es = NULL;
1352 char ip[NI_MAXNUMERICHOST];
1353 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
1354 strlen("client_port=") );
1356 if( httpd_ClientIP( cl, ip ) == NULL )
1358 answer->i_status = 500;
1359 answer->i_body = 0;
1360 answer->p_body = NULL;
1361 break;
1364 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1365 ip, i_port );
1367 psz_session = httpd_MsgGet( query, "Session" );
1368 if( !psz_session || !*psz_session )
1370 char *psz_new;
1371 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1372 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1374 answer->i_status = 503;
1375 answer->i_body = 0;
1376 answer->p_body = NULL;
1377 break;
1379 #warning Session ID should be securely random (spoofing risk)
1380 if( asprintf( &psz_new, "%lu", vlc_mrand48() ) < 0 )
1381 return VLC_ENOMEM;
1382 psz_session = psz_new;
1384 p_rtsp = RtspClientNew( p_media, psz_new );
1385 if( !p_rtsp )
1387 answer->i_status = 454;
1388 answer->i_body = 0;
1389 answer->p_body = NULL;
1390 break;
1393 else
1395 p_rtsp = RtspClientGet( p_media, psz_session );
1396 if( !p_rtsp )
1398 answer->i_status = 454;
1399 answer->i_body = 0;
1400 answer->p_body = NULL;
1401 break;
1405 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1406 if( !p_rtsp_es )
1408 answer->i_status = 500;
1409 answer->i_body = 0;
1410 answer->p_body = NULL;
1411 break;
1413 p_rtsp_es->i_port = i_port;
1414 p_rtsp_es->p_media_es = p_es;
1415 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1417 answer->i_status = 200;
1418 answer->i_body = 0;
1419 answer->p_body = NULL;
1421 if( p_media->b_raw )
1423 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1425 httpd_MsgAdd( answer, "Transport",
1426 "MP2T/H2221/UDP;unicast;client_port=%d-%d",
1427 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1429 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1431 httpd_MsgAdd( answer, "Transport",
1432 "RAW/RAW/UDP;unicast;client_port=%d-%d",
1433 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1436 else
1438 httpd_MsgAdd( answer, "Transport",
1439 "RTP/AVP/UDP;unicast;client_port=%d-%d",
1440 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1443 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1445 answer->i_status = 461;
1446 answer->i_body = 0;
1447 answer->p_body = NULL;
1450 /* Intentional fall-through on x-playNow option in RTSP request */
1451 if( !psz_playnow )
1452 break;
1454 case HTTPD_MSG_PLAY:
1455 /* This is kind of a kludge. Should we only support Aggregate
1456 * Operations ? */
1457 psz_session = httpd_MsgGet( query, "Session" );
1458 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1460 p_rtsp = RtspClientGet( p_media, psz_session );
1462 psz_position = httpd_MsgGet( query, "Range" );
1463 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1464 if( psz_position )
1466 double f_pos = ParseNPT (psz_position + 4);
1467 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1468 f_pos /= ((double)(p_media->i_length)) / CLOCK_FREQ / 100;
1469 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media,
1470 psz_session, f_pos, NULL );
1473 if( !psz_playnow )
1475 answer->i_status = 200;
1476 answer->i_body = 0;
1477 answer->p_body = NULL;
1479 break;
1481 case HTTPD_MSG_TEARDOWN:
1482 answer->i_status = 200;
1483 answer->i_body = 0;
1484 answer->p_body = NULL;
1486 psz_session = httpd_MsgGet( query, "Session" );
1487 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1489 p_rtsp = RtspClientGet( p_media, psz_session );
1490 if( !p_rtsp ) break;
1492 for( int i = 0; i < p_rtsp->i_es; i++ )
1494 if( p_rtsp->es[i]->p_media_es == p_es )
1496 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1497 break;
1501 if( !p_rtsp->i_es )
1503 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session,
1504 0, NULL );
1505 RtspClientDel( p_media, p_rtsp );
1507 break;
1509 case HTTPD_MSG_PAUSE:
1510 /* This is kind of a kludge. Should we only support Aggregate
1511 * Operations ? */
1512 psz_session = httpd_MsgGet( query, "Session" );
1513 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1515 p_rtsp = RtspClientGet( p_media, psz_session );
1516 if( !p_rtsp ) break;
1518 if( !p_rtsp->b_paused )
1520 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session,
1521 0, NULL );
1522 p_rtsp->b_paused = true;
1525 answer->i_status = 200;
1526 answer->i_body = 0;
1527 answer->p_body = NULL;
1528 break;
1530 default:
1531 return VLC_EGENERIC;
1532 break;
1535 httpd_MsgAdd( answer, "Server", "VLC/%s", VERSION );
1536 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1537 psz_cseq = httpd_MsgGet( query, "Cseq" );
1538 if (psz_cseq)
1539 i_cseq = atoi( psz_cseq );
1540 else
1541 i_cseq = 0;
1542 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1543 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1545 if( psz_session )
1546 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1548 return VLC_SUCCESS;
1551 /*****************************************************************************
1552 * SDPGenerate: TODO
1553 * FIXME: need to be moved to a common place ?
1554 *****************************************************************************/
1555 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1557 char *psz_sdp, ip[NI_MAXNUMERICHOST];
1558 const char *psz_control;
1560 if( httpd_ServerIP( cl, ip ) == NULL )
1561 return NULL;
1563 bool ipv6 = ( strchr( ip, ':' ) != NULL );
1565 psz_control = ipv6 ? p_media->psz_rtsp_control_v6
1566 : p_media->psz_rtsp_control_v4;
1568 /* Dummy destination address for RTSP */
1569 struct sockaddr_storage dst;
1570 socklen_t dstlen = ipv6 ? sizeof( struct sockaddr_in6 )
1571 : sizeof( struct sockaddr_in );
1572 memset (&dst, 0, dstlen);
1573 dst.ss_family = ipv6 ? AF_INET6 : AF_INET;
1574 #ifdef HAVE_SA_LEN
1575 dst.ss_len = dstlen;
1576 #endif
1578 psz_sdp = vlc_sdp_Start( VLC_OBJECT( p_media->p_vod ), "sout-rtp-",
1579 NULL, 0, (struct sockaddr *)&dst, dstlen );
1580 if( psz_sdp == NULL )
1581 return NULL;
1583 if( p_media->i_length > 0 )
1585 lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1586 sdp_AddAttribute( &psz_sdp, "range"," npt=0-%lld.%03u", d.quot,
1587 (unsigned)d.rem );
1590 for( int i = 0; i < p_media->i_es; i++ )
1592 media_es_t *p_es = p_media->es[i];
1593 const char *mime_major; /* major MIME type */
1595 switch( p_es->fmt.i_cat )
1597 case VIDEO_ES:
1598 mime_major = "video";
1599 break;
1600 case AUDIO_ES:
1601 mime_major = "audio";
1602 break;
1603 case SPU_ES:
1604 mime_major = "text";
1605 break;
1606 default:
1607 continue;
1610 sdp_AddMedia( &psz_sdp, mime_major, "RTP/AVP", 0 /* p_es->i_port */,
1611 p_es->i_payload_type, false, 0,
1612 p_es->psz_ptname, p_es->i_clock_rate, p_es->i_channels,
1613 p_es->psz_fmtp );
1615 sdp_AddAttribute( &psz_sdp, "control", psz_control, ip, i );
1618 return psz_sdp;