1 /*****************************************************************************
2 * access.c: RTMP input.
3 *****************************************************************************
4 * Copyright (C) URJC - LADyR - Luis Lopez Fernandez
6 * Author: Miguel Angel Cabrera Moya
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_access.h>
34 #include <vlc_network.h> /* DOWN: #include <network.h> */
36 #include <vlc_block.h>
38 #include "rtmp_amf_flv.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 #define CACHING_TEXT N_("Caching value in ms")
44 #define CACHING_LONGTEXT N_( \
45 "Caching value for RTMP streams. This " \
46 "value should be set in milliseconds." )
48 #define SWFURL_TEXT N_("Default SWF Referrer URL")
49 #define SWFURL_LONGTEXT N_("The SFW URL to use as referrer when connecting to "\
50 "the server. This is the SWF file that contained " \
53 #define PAGEURL_TEXT N_("Default Page Referrer URL")
54 #define PAGEURL_LONGTEXT N_("The Page URL to use as referrer when connecting to " \
55 "the server. This is the page housing the SWF " \
58 static int Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
62 set_description( N_("RTMP input") )
63 set_shortname( N_("RTMP") )
64 set_category( CAT_INPUT )
65 set_subcategory( SUBCAT_INPUT_ACCESS )
67 add_integer( "rtmp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
68 CACHING_LONGTEXT, true )
69 add_string( "rtmp-swfurl", "file:///player.swf", NULL, SWFURL_TEXT,
70 SWFURL_LONGTEXT, true )
71 add_string( "rtmp-pageurl", "file:///player.htm", NULL, PAGEURL_TEXT,
72 PAGEURL_LONGTEXT, true )
74 set_capability( "access", 0 )
75 set_callbacks( Open, Close )
76 add_shortcut( "rtmp" )
80 /*****************************************************************************
82 *****************************************************************************/
83 static ssize_t Read( access_t *, uint8_t *, size_t );
84 static int Seek( access_t *, int64_t );
85 static int Control( access_t *, int, va_list );
87 static void* ThreadControl( void * );
89 /*****************************************************************************
90 * Open: open the rtmp connection
91 *****************************************************************************/
92 static int Open( vlc_object_t *p_this )
94 access_t *p_access = (access_t *) p_this;
97 int length_path, length_media_name;
100 STANDARD_READ_ACCESS_INIT
103 vlc_object_create( p_access, sizeof( rtmp_control_thread_t ) );
104 if( !p_sys->p_thread )
106 vlc_object_attach( p_sys->p_thread, p_access );
108 /* Parse URI - remove spaces */
109 p = psz = strdup( p_access->psz_path );
110 while( (p = strchr( p, ' ' )) != NULL )
112 vlc_UrlParse( &p_sys->p_thread->url, psz, 0 );
115 if( p_sys->p_thread->url.psz_host == NULL
116 || *p_sys->p_thread->url.psz_host == '\0' )
118 msg_Warn( p_access, "invalid host" );
122 if( p_sys->p_thread->url.i_port <= 0 )
123 p_sys->p_thread->url.i_port = 1935;
125 if( p_sys->p_thread->url.psz_path == NULL )
127 msg_Warn( p_access, "invalid path" );
131 length_path = strlen( p_sys->p_thread->url.psz_path );
132 char* psz_tmp = strrchr( p_sys->p_thread->url.psz_path, '/' );
135 length_media_name = strlen( psz_tmp ) - 1;
137 p_sys->p_thread->psz_application = strndup( p_sys->p_thread->url.psz_path + 1, length_path - length_media_name - 2 );
138 p_sys->p_thread->psz_media = strdup( p_sys->p_thread->url.psz_path + ( length_path - length_media_name ) );
140 p_sys->p_thread->psz_swf_url = var_CreateGetString( p_access, "rtmp-swfurl" );
141 p_sys->p_thread->psz_page_url = var_CreateGetString( p_access, "rtmp-pageurl" );
143 msg_Dbg( p_access, "rtmp: host='%s' port=%d path='%s'",
144 p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port, p_sys->p_thread->url.psz_path );
146 if( p_sys->p_thread->url.psz_username && *p_sys->p_thread->url.psz_username )
148 msg_Dbg( p_access, " user='%s'", p_sys->p_thread->url.psz_username );
151 /* Initialize thread variables */
152 p_sys->p_thread->b_error= 0;
153 p_sys->p_thread->p_fifo_input = block_FifoNew();
154 p_sys->p_thread->p_empty_blocks = block_FifoNew();
155 p_sys->p_thread->has_audio = 0;
156 p_sys->p_thread->has_video = 0;
157 p_sys->p_thread->metadata_received = 0;
158 p_sys->p_thread->first_media_packet = 1;
159 p_sys->p_thread->flv_tag_previous_tag_size = 0x00000000; /* FLV_TAG_FIRST_PREVIOUS_TAG_SIZE */
160 p_sys->p_thread->chunk_size_recv = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
161 p_sys->p_thread->chunk_size_send = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
162 for(i = 0; i < 64; i++)
164 memset( &p_sys->p_thread->rtmp_headers_recv[i], 0, sizeof( rtmp_packet_t ) );
165 p_sys->p_thread->rtmp_headers_send[i].length_header = -1;
166 p_sys->p_thread->rtmp_headers_send[i].stream_index = -1;
167 p_sys->p_thread->rtmp_headers_send[i].timestamp = -1;
168 p_sys->p_thread->rtmp_headers_send[i].timestamp_relative = -1;
169 p_sys->p_thread->rtmp_headers_send[i].length_encoded = -1;
170 p_sys->p_thread->rtmp_headers_send[i].length_body = -1;
171 p_sys->p_thread->rtmp_headers_send[i].content_type = -1;
172 p_sys->p_thread->rtmp_headers_send[i].src_dst = -1;
173 p_sys->p_thread->rtmp_headers_send[i].body = NULL;
176 p_sys->p_thread->p_base_object = p_this;
178 vlc_cond_init( &p_sys->p_thread->wait );
180 vlc_mutex_init( &p_sys->p_thread->lock );
182 p_sys->p_thread->result_connect = 1;
183 p_sys->p_thread->result_play = 1;
184 p_sys->p_thread->result_stop = 0;
186 /* Open connection */
187 p_sys->p_thread->fd = net_ConnectTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
188 if( p_sys->p_thread->fd == -1 )
192 msg_Warn( p_access, "cannot connect to %s:%d", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
193 msg_Dbg( p_access, "switching to passive mode" );
197 p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
198 if( p_fd_listen == NULL )
200 msg_Err( p_access, "cannot listen to %s port %i", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
204 p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen );
206 net_ListenClose( p_fd_listen );
208 if( rtmp_handshake_passive( p_this, p_sys->p_thread->fd ) < 0 )
210 msg_Err( p_access, "handshake passive failed");
214 p_sys->p_thread->result_publish = 1;
220 if( rtmp_handshake_active( p_this, p_sys->p_thread->fd ) < 0 )
222 msg_Err( p_access, "handshake active failed");
226 p_sys->p_thread->result_publish = 0;
229 if( vlc_clone( &p_sys->p_thread->thread, ThreadControl, p_sys->p_thread,
230 VLC_THREAD_PRIORITY_INPUT ) )
232 msg_Err( p_access, "cannot spawn rtmp control thread" );
238 if( rtmp_connect_active( p_sys->p_thread ) < 0 )
240 msg_Err( p_access, "connect active failed");
241 /* Kill the running thread */
242 vlc_cancel( p_sys->p_thread->thread );
243 vlc_join( p_sys->p_thread->thread, NULL );
248 /* Set vars for reading from fifo */
249 p_access->p_sys->flv_packet = NULL;
250 p_access->p_sys->read_packet = 1;
252 /* Update default_pts to a suitable value for rtmp access */
253 var_Create( p_access, "rtmp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
258 vlc_cond_destroy( &p_sys->p_thread->wait );
259 vlc_mutex_destroy( &p_sys->p_thread->lock );
261 block_FifoRelease( p_sys->p_thread->p_fifo_input );
262 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
264 free( p_sys->p_thread->psz_application );
265 free( p_sys->p_thread->psz_media );
266 free( p_sys->p_thread->psz_swf_url );
267 free( p_sys->p_thread->psz_page_url );
269 net_Close( p_sys->p_thread->fd );
271 vlc_UrlClean( &p_sys->p_thread->url );
273 vlc_object_detach( p_sys->p_thread );
274 vlc_object_release( p_sys->p_thread );
281 /*****************************************************************************
282 * Close: close the rtmp connection
283 *****************************************************************************/
284 static void Close( vlc_object_t * p_this )
286 access_t *p_access = (access_t *) p_this;
287 access_sys_t *p_sys = p_access->p_sys;
290 vlc_cancel( p_sys->p_thread->thread );
291 vlc_join( p_sys->p_thread->thread, NULL );
293 vlc_cond_destroy( &p_sys->p_thread->wait );
294 vlc_mutex_destroy( &p_sys->p_thread->lock );
296 block_FifoRelease( p_sys->p_thread->p_fifo_input );
297 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
299 for( i = 0; i < 64; i++ ) /* RTMP_HEADER_STREAM_INDEX_MASK */
301 if( p_sys->p_thread->rtmp_headers_recv[i].body != NULL )
303 free( p_sys->p_thread->rtmp_headers_recv[i].body->body );
304 free( p_sys->p_thread->rtmp_headers_recv[i].body );
308 net_Close( p_sys->p_thread->fd );
310 var_Destroy( p_access, "rtmp-caching" );
312 vlc_UrlClean( &p_sys->p_thread->url );
313 free( p_sys->p_thread->psz_application );
314 free( p_sys->p_thread->psz_media );
315 free( p_sys->p_thread->psz_swf_url );
316 free( p_sys->p_thread->psz_page_url );
318 vlc_object_detach( p_sys->p_thread );
319 vlc_object_release( p_sys->p_thread );
323 /*****************************************************************************
324 * Read: standard read on a file descriptor.
325 *****************************************************************************/
326 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
328 access_sys_t *p_sys = p_access->p_sys;
329 rtmp_packet_t *rtmp_packet;
336 while( i_len_tmp < i_len )
338 if( p_sys->p_thread->result_stop || p_access->info.b_eof || !vlc_object_alive (p_access) )
340 p_access->info.b_eof = true;
344 if( p_sys->read_packet )
346 if( !p_sys->p_thread->metadata_received )
348 /* Wait until enough data is received for extracting metadata */
349 #warning This is not thread-safe (because block_FifoCount() is not)!
350 if( block_FifoCount( p_sys->p_thread->p_fifo_input ) < 10 )
352 #warning This is wrong!
357 p_sys->flv_packet = flv_get_metadata( p_access );
359 p_sys->p_thread->metadata_received = 1;
363 p_sys->flv_packet = block_FifoGet( p_sys->p_thread->p_fifo_input );
364 if( p_sys->flv_packet == NULL )
365 continue; /* Forced wake-up */
368 if( p_sys->p_thread->first_media_packet )
370 p_sys->flv_packet = flv_insert_header( p_access, p_sys->flv_packet );
372 p_sys->p_thread->first_media_packet = 0;
375 if( i_len - i_len_tmp >= p_sys->flv_packet->i_buffer )
377 p_sys->read_packet = 1;
379 memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, p_sys->flv_packet->i_buffer );
380 block_FifoPut( p_sys->p_thread->p_empty_blocks, p_sys->flv_packet );
382 i_len_tmp += p_sys->flv_packet->i_buffer;
386 p_sys->read_packet = 0;
388 memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, i_len - i_len_tmp);
389 p_sys->flv_packet->i_buffer -= i_len - i_len_tmp;
390 memmove( p_sys->flv_packet->p_buffer, p_sys->flv_packet->p_buffer + i_len - i_len_tmp, p_sys->flv_packet->i_buffer );
392 i_len_tmp += i_len - i_len_tmp;
397 if( p_sys->p_thread->result_publish )
399 /* Send publish onStatus event only once */
400 p_sys->p_thread->result_publish = 0;
402 rtmp_packet = rtmp_build_publish_start( p_sys->p_thread );
404 tmp_buffer = rtmp_encode_packet( p_sys->p_thread, rtmp_packet );
406 i_ret = net_Write( p_sys->p_thread, p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
407 free( rtmp_packet->body->body );
408 free( rtmp_packet->body );
411 if( i_ret != rtmp_packet->length_encoded )
413 msg_Err( p_access, "failed send publish start" );
418 p_access->info.i_pos += i_len_tmp;
420 rtmp_packet = rtmp_build_bytes_read( p_sys->p_thread, p_access->info.i_pos );
422 tmp_buffer = rtmp_encode_packet( p_sys->p_thread, rtmp_packet );
424 i_ret = net_Write( p_sys->p_thread, p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
425 free( rtmp_packet->body->body );
426 free( rtmp_packet->body );
430 if( i_ret != rtmp_packet->length_encoded )
432 msg_Err( p_access, "failed send bytes read" );
440 /*****************************************************************************
441 * Seek: seek to a specific location in a file
442 *****************************************************************************/
443 static int Seek( access_t *p_access, int64_t i_pos )
445 VLC_UNUSED( p_access );
447 /*msg_Warn ( p_access, "Seek to %lld", i_pos);
448 switch( rtmp_seek( p_access, i_pos ) )
455 msg_Err( p_access, "You should not be here" );
462 /*****************************************************************************
464 *****************************************************************************/
465 static int Control( access_t *p_access, int i_query, va_list args )
473 case ACCESS_CAN_SEEK:
474 case ACCESS_CAN_FASTSEEK:
475 pb_bool = (bool*) va_arg( args, bool* );
476 *pb_bool = false; /* TODO */
479 case ACCESS_CAN_PAUSE:
480 pb_bool = (bool*) va_arg( args, bool* );
481 *pb_bool = false; /* TODO */
484 case ACCESS_CAN_CONTROL_PACE:
485 pb_bool = (bool*) va_arg( args, bool* );
490 case ACCESS_GET_PTS_DELAY:
491 pi_64 = (int64_t*) va_arg( args, int64_t * );
492 *pi_64 = var_GetInteger( p_access, "rtmp-caching" ) * INT64_C(1000);
496 case ACCESS_SET_PAUSE_STATE:
500 case ACCESS_GET_TITLE_INFO:
501 case ACCESS_SET_TITLE:
502 case ACCESS_SET_SEEKPOINT:
503 case ACCESS_SET_PRIVATE_ID_STATE:
504 case ACCESS_GET_META:
505 case ACCESS_GET_CONTENT_TYPE: /* DOWN: comment this line */
509 msg_Warn( p_access, "unimplemented query in control" );
516 /*****************************************************************************
517 * ThreadControl: manage control messages and pipe media to Read
518 *****************************************************************************/
519 static void* ThreadControl( void *p_this )
521 rtmp_control_thread_t *p_thread = p_this;
522 rtmp_packet_t *rtmp_packet;
523 int canc = vlc_savecancel ();
525 rtmp_init_handler( p_thread->rtmp_handler );
529 vlc_restorecancel( canc );
530 rtmp_packet = rtmp_read_net_packet( p_thread );
531 canc = vlc_savecancel( );
532 if( rtmp_packet != NULL )
534 if( rtmp_packet->content_type < 0x01 /* RTMP_CONTENT_TYPE_CHUNK_SIZE */
535 || rtmp_packet->content_type > 0x14 ) /* RTMP_CONTENT_TYPE_INVOKE */
537 free( rtmp_packet->body->body );
538 free( rtmp_packet->body );
541 msg_Warn( p_thread, "unknown content type received" );
544 p_thread->rtmp_handler[rtmp_packet->content_type]( p_thread, rtmp_packet );
548 /* Sometimes server close connection too soon */
549 if( p_thread->result_connect )
551 #warning There must be a bug here!
552 vlc_mutex_lock( &p_thread->lock );
553 vlc_cond_signal( &p_thread->wait );
554 vlc_mutex_unlock( &p_thread->lock );
557 #warning info cannot be accessed outside input thread!
558 ((access_t *) p_thread->p_base_object)->info.b_eof = true;
559 block_FifoWake( p_thread->p_fifo_input );
563 vlc_restorecancel (canc);