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 SWFURL_TEXT N_("Default SWF Referrer URL")
44 #define SWFURL_LONGTEXT N_("The SFW URL to use as referrer when connecting to "\
45 "the server. This is the SWF file that contained " \
48 #define PAGEURL_TEXT N_("Default Page Referrer URL")
49 #define PAGEURL_LONGTEXT N_("The Page URL to use as referrer when connecting to " \
50 "the server. This is the page housing the SWF " \
53 static int Open ( vlc_object_t * );
54 static void Close( vlc_object_t * );
57 set_description( N_("RTMP input") )
58 set_shortname( N_("RTMP") )
59 set_category( CAT_INPUT )
60 set_subcategory( SUBCAT_INPUT_ACCESS )
62 add_string( "rtmp-swfurl", "file:///player.swf", SWFURL_TEXT,
63 SWFURL_LONGTEXT, true )
64 add_string( "rtmp-pageurl", "file:///player.htm", PAGEURL_TEXT,
65 PAGEURL_LONGTEXT, true )
67 set_capability( "access", 0 )
68 set_callbacks( Open, Close )
69 add_shortcut( "rtmp" )
73 /*****************************************************************************
75 *****************************************************************************/
76 static ssize_t Read( access_t *, uint8_t *, size_t );
77 static int Seek( access_t *, uint64_t );
78 static int Control( access_t *, int, va_list );
80 static void* ThreadControl( void * );
82 /*****************************************************************************
83 * Open: open the rtmp connection
84 *****************************************************************************/
85 static int Open( vlc_object_t *p_this )
87 access_t *p_access = (access_t *) p_this;
90 int length_path, length_media_name;
93 STANDARD_READ_ACCESS_INIT
96 vlc_object_create( p_access, sizeof( rtmp_control_thread_t ) );
97 if( !p_sys->p_thread )
100 /* Parse URI - remove spaces */
101 p = psz = strdup( p_access->psz_location );
102 while( (p = strchr( p, ' ' )) != NULL )
104 vlc_UrlParse( &p_sys->p_thread->url, psz, 0 );
107 if( p_sys->p_thread->url.psz_host == NULL
108 || *p_sys->p_thread->url.psz_host == '\0' )
110 msg_Warn( p_access, "invalid host" );
114 if( p_sys->p_thread->url.i_port <= 0 )
115 p_sys->p_thread->url.i_port = 1935;
117 if( p_sys->p_thread->url.psz_path == NULL )
119 msg_Warn( p_access, "invalid path" );
123 length_path = strlen( p_sys->p_thread->url.psz_path );
124 char* psz_tmp = strrchr( p_sys->p_thread->url.psz_path, '/' );
127 length_media_name = strlen( psz_tmp ) - 1;
129 p_sys->p_thread->psz_application = strndup( p_sys->p_thread->url.psz_path + 1, length_path - length_media_name - 2 );
130 p_sys->p_thread->psz_media = strdup( p_sys->p_thread->url.psz_path + ( length_path - length_media_name ) );
132 p_sys->p_thread->psz_swf_url = var_CreateGetString( p_access, "rtmp-swfurl" );
133 p_sys->p_thread->psz_page_url = var_CreateGetString( p_access, "rtmp-pageurl" );
135 msg_Dbg( p_access, "rtmp: host='%s' port=%d path='%s'",
136 p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port, p_sys->p_thread->url.psz_path );
138 if( p_sys->p_thread->url.psz_username && *p_sys->p_thread->url.psz_username )
140 msg_Dbg( p_access, " user='%s'", p_sys->p_thread->url.psz_username );
143 /* Initialize thread variables */
144 p_sys->p_thread->b_error= 0;
145 p_sys->p_thread->p_fifo_input = block_FifoNew();
146 p_sys->p_thread->p_empty_blocks = block_FifoNew();
147 p_sys->p_thread->has_audio = 0;
148 p_sys->p_thread->has_video = 0;
149 p_sys->p_thread->metadata_received = 0;
150 p_sys->p_thread->first_media_packet = 1;
151 p_sys->p_thread->flv_tag_previous_tag_size = 0x00000000; /* FLV_TAG_FIRST_PREVIOUS_TAG_SIZE */
152 p_sys->p_thread->chunk_size_recv = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
153 p_sys->p_thread->chunk_size_send = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
154 for(i = 0; i < 64; i++)
156 memset( &p_sys->p_thread->rtmp_headers_recv[i], 0, sizeof( rtmp_packet_t ) );
157 p_sys->p_thread->rtmp_headers_send[i].length_header = -1;
158 p_sys->p_thread->rtmp_headers_send[i].stream_index = -1;
159 p_sys->p_thread->rtmp_headers_send[i].timestamp = -1;
160 p_sys->p_thread->rtmp_headers_send[i].timestamp_relative = -1;
161 p_sys->p_thread->rtmp_headers_send[i].length_encoded = -1;
162 p_sys->p_thread->rtmp_headers_send[i].length_body = -1;
163 p_sys->p_thread->rtmp_headers_send[i].content_type = -1;
164 p_sys->p_thread->rtmp_headers_send[i].src_dst = -1;
165 p_sys->p_thread->rtmp_headers_send[i].body = NULL;
168 p_sys->p_thread->p_base_object = p_this;
170 vlc_cond_init( &p_sys->p_thread->wait );
172 vlc_mutex_init( &p_sys->p_thread->lock );
174 p_sys->p_thread->result_connect = 1;
175 p_sys->p_thread->result_play = 1;
176 p_sys->p_thread->result_stop = 0;
178 /* Open connection */
179 p_sys->p_thread->fd = net_ConnectTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
180 if( p_sys->p_thread->fd == -1 )
184 msg_Warn( p_access, "cannot connect to %s:%d", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
185 msg_Dbg( p_access, "switching to passive mode" );
189 p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
190 if( p_fd_listen == NULL )
192 msg_Err( p_access, "cannot listen to %s port %i", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
196 p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen );
198 net_ListenClose( p_fd_listen );
200 if( rtmp_handshake_passive( p_this, p_sys->p_thread->fd ) < 0 )
202 msg_Err( p_access, "handshake passive failed");
206 p_sys->p_thread->result_publish = 1;
212 if( rtmp_handshake_active( p_this, p_sys->p_thread->fd ) < 0 )
214 msg_Err( p_access, "handshake active failed");
218 p_sys->p_thread->result_publish = 0;
221 if( vlc_clone( &p_sys->p_thread->thread, ThreadControl, p_sys->p_thread,
222 VLC_THREAD_PRIORITY_INPUT ) )
224 msg_Err( p_access, "cannot spawn rtmp control thread" );
230 if( rtmp_connect_active( p_sys->p_thread ) < 0 )
232 msg_Err( p_access, "connect active failed");
233 /* Kill the running thread */
234 vlc_cancel( p_sys->p_thread->thread );
235 vlc_join( p_sys->p_thread->thread, NULL );
240 /* Set vars for reading from fifo */
241 p_access->p_sys->flv_packet = NULL;
242 p_access->p_sys->read_packet = 1;
247 vlc_cond_destroy( &p_sys->p_thread->wait );
248 vlc_mutex_destroy( &p_sys->p_thread->lock );
250 block_FifoRelease( p_sys->p_thread->p_fifo_input );
251 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
253 free( p_sys->p_thread->psz_application );
254 free( p_sys->p_thread->psz_media );
255 free( p_sys->p_thread->psz_swf_url );
256 free( p_sys->p_thread->psz_page_url );
258 net_Close( p_sys->p_thread->fd );
260 vlc_UrlClean( &p_sys->p_thread->url );
262 vlc_object_release( p_sys->p_thread );
269 /*****************************************************************************
270 * Close: close the rtmp connection
271 *****************************************************************************/
272 static void Close( vlc_object_t * p_this )
274 access_t *p_access = (access_t *) p_this;
275 access_sys_t *p_sys = p_access->p_sys;
278 vlc_cancel( p_sys->p_thread->thread );
279 vlc_join( p_sys->p_thread->thread, NULL );
281 vlc_cond_destroy( &p_sys->p_thread->wait );
282 vlc_mutex_destroy( &p_sys->p_thread->lock );
284 block_FifoRelease( p_sys->p_thread->p_fifo_input );
285 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
287 for( i = 0; i < 64; i++ ) /* RTMP_HEADER_STREAM_INDEX_MASK */
289 if( p_sys->p_thread->rtmp_headers_recv[i].body != NULL )
291 free( p_sys->p_thread->rtmp_headers_recv[i].body->body );
292 free( p_sys->p_thread->rtmp_headers_recv[i].body );
296 net_Close( p_sys->p_thread->fd );
298 vlc_UrlClean( &p_sys->p_thread->url );
299 free( p_sys->p_thread->psz_application );
300 free( p_sys->p_thread->psz_media );
301 free( p_sys->p_thread->psz_swf_url );
302 free( p_sys->p_thread->psz_page_url );
304 vlc_object_release( p_sys->p_thread );
308 /*****************************************************************************
309 * Read: standard read on a file descriptor.
310 *****************************************************************************/
311 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
313 access_sys_t *p_sys = p_access->p_sys;
314 rtmp_packet_t *rtmp_packet;
321 while( i_len_tmp < i_len )
323 if( p_sys->p_thread->result_stop || p_access->info.b_eof || !vlc_object_alive (p_access) )
325 p_access->info.b_eof = true;
329 if( p_sys->read_packet )
331 if( !p_sys->p_thread->metadata_received )
333 /* Wait until enough data is received for extracting metadata */
334 #warning This is not thread-safe (because block_FifoCount() is not)!
335 if( block_FifoCount( p_sys->p_thread->p_fifo_input ) < 10 )
337 #warning This is wrong!
342 p_sys->flv_packet = flv_get_metadata( p_access );
344 p_sys->p_thread->metadata_received = 1;
348 p_sys->flv_packet = block_FifoGet( p_sys->p_thread->p_fifo_input );
349 if( p_sys->flv_packet == NULL )
350 continue; /* Forced wake-up */
353 if( p_sys->p_thread->first_media_packet )
355 p_sys->flv_packet = flv_insert_header( p_access, p_sys->flv_packet );
357 p_sys->p_thread->first_media_packet = 0;
360 if( i_len - i_len_tmp >= p_sys->flv_packet->i_buffer )
362 p_sys->read_packet = 1;
364 memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, p_sys->flv_packet->i_buffer );
365 block_FifoPut( p_sys->p_thread->p_empty_blocks, p_sys->flv_packet );
367 i_len_tmp += p_sys->flv_packet->i_buffer;
371 p_sys->read_packet = 0;
373 memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, i_len - i_len_tmp);
374 p_sys->flv_packet->i_buffer -= i_len - i_len_tmp;
375 memmove( p_sys->flv_packet->p_buffer, p_sys->flv_packet->p_buffer + i_len - i_len_tmp, p_sys->flv_packet->i_buffer );
377 i_len_tmp += i_len - i_len_tmp;
382 if( p_sys->p_thread->result_publish )
384 /* Send publish onStatus event only once */
385 p_sys->p_thread->result_publish = 0;
387 rtmp_packet = rtmp_build_publish_start( p_sys->p_thread );
389 tmp_buffer = rtmp_encode_packet( p_sys->p_thread, rtmp_packet );
391 i_ret = net_Write( p_sys->p_thread, p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
392 if( i_ret != rtmp_packet->length_encoded )
394 msg_Err( p_access, "failed send publish start" );
397 free( rtmp_packet->body->body );
398 free( rtmp_packet->body );
403 p_access->info.i_pos += i_len_tmp;
405 rtmp_packet = rtmp_build_bytes_read( p_sys->p_thread, p_access->info.i_pos );
407 tmp_buffer = rtmp_encode_packet( p_sys->p_thread, rtmp_packet );
409 i_ret = net_Write( p_sys->p_thread, p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
410 if( i_ret != rtmp_packet->length_encoded )
412 msg_Err( p_access, "failed send bytes read" );
415 free( rtmp_packet->body->body );
416 free( rtmp_packet->body );
424 free( rtmp_packet->body->body );
425 free( rtmp_packet->body );
431 /*****************************************************************************
432 * Seek: seek to a specific location in a file
433 *****************************************************************************/
434 static int Seek( access_t *p_access, uint64_t i_pos )
436 VLC_UNUSED( p_access );
438 /*msg_Warn ( p_access, "Seek to %lld", i_pos);
439 switch( rtmp_seek( p_access, i_pos ) )
446 msg_Err( p_access, "You should not be here" );
453 /*****************************************************************************
455 *****************************************************************************/
456 static int Control( access_t *p_access, int i_query, va_list args )
464 case ACCESS_CAN_SEEK:
465 case ACCESS_CAN_FASTSEEK:
466 pb_bool = (bool*) va_arg( args, bool* );
467 *pb_bool = false; /* TODO */
470 case ACCESS_CAN_PAUSE:
471 pb_bool = (bool*) va_arg( args, bool* );
472 *pb_bool = false; /* TODO */
475 case ACCESS_CAN_CONTROL_PACE:
476 pb_bool = (bool*) va_arg( args, bool* );
481 case ACCESS_GET_PTS_DELAY:
482 pi_64 = (int64_t*) va_arg( args, int64_t * );
483 *pi_64 = INT64_C(1000)
484 * var_InheritInteger( p_access, "network-caching" );
488 case ACCESS_SET_PAUSE_STATE:
492 case ACCESS_GET_TITLE_INFO:
493 case ACCESS_SET_TITLE:
494 case ACCESS_SET_SEEKPOINT:
495 case ACCESS_SET_PRIVATE_ID_STATE:
496 case ACCESS_GET_META:
497 case ACCESS_GET_CONTENT_TYPE: /* DOWN: comment this line */
501 msg_Warn( p_access, "unimplemented query in control" );
508 /*****************************************************************************
509 * ThreadControl: manage control messages and pipe media to Read
510 *****************************************************************************/
511 static void* ThreadControl( void *p_this )
513 rtmp_control_thread_t *p_thread = p_this;
514 rtmp_packet_t *rtmp_packet;
515 int canc = vlc_savecancel ();
517 rtmp_init_handler( p_thread->rtmp_handler );
521 vlc_restorecancel( canc );
522 rtmp_packet = rtmp_read_net_packet( p_thread );
523 canc = vlc_savecancel( );
524 if( rtmp_packet != NULL )
526 if( rtmp_packet->content_type < 0x01 /* RTMP_CONTENT_TYPE_CHUNK_SIZE */
527 || rtmp_packet->content_type > 0x14 ) /* RTMP_CONTENT_TYPE_INVOKE */
529 free( rtmp_packet->body->body );
530 free( rtmp_packet->body );
533 msg_Warn( p_thread, "unknown content type received" );
536 p_thread->rtmp_handler[rtmp_packet->content_type]( p_thread, rtmp_packet );
540 /* Sometimes server close connection too soon */
541 if( p_thread->result_connect )
543 #warning There must be a bug here!
544 vlc_mutex_lock( &p_thread->lock );
545 vlc_cond_signal( &p_thread->wait );
546 vlc_mutex_unlock( &p_thread->lock );
549 #warning info cannot be accessed outside input thread!
550 ((access_t *) p_thread->p_base_object)->info.b_eof = true;
551 block_FifoWake( p_thread->p_fifo_input );
555 vlc_restorecancel (canc);