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 static int Open ( vlc_object_t * );
49 static void Close( vlc_object_t * );
52 set_description( N_("RTMP input") )
53 set_shortname( N_("RTMP") )
54 set_category( CAT_INPUT )
55 set_subcategory( SUBCAT_INPUT_ACCESS )
57 add_integer( "rtmp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
58 CACHING_LONGTEXT, true );
60 set_capability( "access", 0 )
61 set_callbacks( Open, Close )
62 add_shortcut( "rtmp" )
66 /*****************************************************************************
68 *****************************************************************************/
69 static ssize_t Read( access_t *, uint8_t *, size_t );
70 static int Seek( access_t *, int64_t );
71 static int Control( access_t *, int, va_list );
73 static void* ThreadControl( vlc_object_t * );
75 /*****************************************************************************
76 * Open: open the rtmp connection
77 *****************************************************************************/
78 static int Open( vlc_object_t *p_this )
80 access_t *p_access = (access_t *) p_this;
83 int length_path, length_media_name;
86 STANDARD_READ_ACCESS_INIT
89 vlc_object_create( p_access, sizeof( rtmp_control_thread_t ) );
90 if( !p_sys->p_thread )
92 vlc_object_attach( p_sys->p_thread, p_access );
94 /* Parse URI - remove spaces */
95 p = psz = strdup( p_access->psz_path );
96 while( (p = strchr( p, ' ' )) != NULL )
98 vlc_UrlParse( &p_sys->p_thread->url, psz, 0 );
101 if( p_sys->p_thread->url.psz_host == NULL
102 || *p_sys->p_thread->url.psz_host == '\0' )
104 msg_Warn( p_access, "invalid host" );
108 if( p_sys->p_thread->url.i_port <= 0 )
109 p_sys->p_thread->url.i_port = 1935;
111 if( p_sys->p_thread->url.psz_path == NULL )
113 msg_Warn( p_access, "invalid path" );
117 length_path = strlen( p_sys->p_thread->url.psz_path );
118 char* psz_tmp = strrchr( p_sys->p_thread->url.psz_path, '/' );
121 length_media_name = strlen( psz_tmp ) - 1;
123 p_sys->p_thread->psz_application = strndup( p_sys->p_thread->url.psz_path + 1, length_path - length_media_name - 2 );
124 p_sys->p_thread->psz_media = strdup( p_sys->p_thread->url.psz_path + ( length_path - length_media_name ) );
126 msg_Dbg( p_access, "rtmp: host='%s' port=%d path='%s'",
127 p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port, p_sys->p_thread->url.psz_path );
129 if( p_sys->p_thread->url.psz_username && *p_sys->p_thread->url.psz_username )
131 msg_Dbg( p_access, " user='%s', pwd='%s'",
132 p_sys->p_thread->url.psz_username, p_sys->p_thread->url.psz_password );
135 /* Initialize thread variables */
136 p_sys->p_thread->b_die = 0;
137 p_sys->p_thread->b_error= 0;
138 p_sys->p_thread->p_fifo_input = block_FifoNew();
139 p_sys->p_thread->p_empty_blocks = block_FifoNew();
140 p_sys->p_thread->has_audio = 0;
141 p_sys->p_thread->has_video = 0;
142 p_sys->p_thread->metadata_received = 0;
143 p_sys->p_thread->first_media_packet = 1;
144 p_sys->p_thread->flv_tag_previous_tag_size = 0x00000000; /* FLV_TAG_FIRST_PREVIOUS_TAG_SIZE */
145 p_sys->p_thread->chunk_size_recv = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
146 p_sys->p_thread->chunk_size_send = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
147 for(i = 0; i < 64; i++)
149 memset( &p_sys->p_thread->rtmp_headers_recv[i], 0, sizeof( rtmp_packet_t ) );
150 p_sys->p_thread->rtmp_headers_send[i].length_header = -1;
151 p_sys->p_thread->rtmp_headers_send[i].stream_index = -1;
152 p_sys->p_thread->rtmp_headers_send[i].timestamp = -1;
153 p_sys->p_thread->rtmp_headers_send[i].timestamp_relative = -1;
154 p_sys->p_thread->rtmp_headers_send[i].length_encoded = -1;
155 p_sys->p_thread->rtmp_headers_send[i].length_body = -1;
156 p_sys->p_thread->rtmp_headers_send[i].content_type = -1;
157 p_sys->p_thread->rtmp_headers_send[i].src_dst = -1;
158 p_sys->p_thread->rtmp_headers_send[i].body = NULL;
161 p_sys->p_thread->p_base_object = p_this;
163 vlc_cond_init( &p_sys->p_thread->wait );
165 vlc_mutex_init( &p_sys->p_thread->lock );
167 p_sys->p_thread->result_connect = 1;
168 p_sys->p_thread->result_play = 1;
169 p_sys->p_thread->result_stop = 0;
171 /* Open connection */
172 p_sys->p_thread->fd = net_ConnectTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
173 if( p_sys->p_thread->fd == -1 )
177 msg_Warn( p_access, "cannot connect to %s:%d", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
178 msg_Dbg( p_access, "switching to passive mode" );
182 p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
183 if( p_fd_listen == NULL )
185 msg_Err( p_access, "cannot listen to %s port %i", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
189 p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen, -1 );
191 net_ListenClose( p_fd_listen );
193 if( rtmp_handshake_passive( p_this, p_sys->p_thread->fd ) < 0 )
195 msg_Err( p_access, "handshake passive failed");
199 p_sys->p_thread->result_publish = 1;
205 if( rtmp_handshake_active( p_this, p_sys->p_thread->fd ) < 0 )
207 msg_Err( p_access, "handshake active failed");
211 p_sys->p_thread->result_publish = 0;
214 if( vlc_thread_create( p_sys->p_thread, "rtmp control thread", ThreadControl,
215 VLC_THREAD_PRIORITY_INPUT ) )
217 msg_Err( p_access, "cannot spawn rtmp control thread" );
223 if( rtmp_connect_active( p_sys->p_thread ) < 0 )
225 msg_Err( p_access, "connect active failed");
230 /* Set vars for reading from fifo */
231 p_access->p_sys->flv_packet = NULL;
232 p_access->p_sys->read_packet = 1;
234 /* Update default_pts to a suitable value for rtmp access */
235 var_Create( p_access, "rtmp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
240 vlc_cond_destroy( &p_sys->p_thread->wait );
241 vlc_mutex_destroy( &p_sys->p_thread->lock );
243 free( p_sys->p_thread->psz_application );
244 free( p_sys->p_thread->psz_media );
246 net_Close( p_sys->p_thread->fd );
248 vlc_UrlClean( &p_sys->p_thread->url );
250 vlc_object_detach( p_sys->p_thread );
251 vlc_object_release( p_sys->p_thread );
258 /*****************************************************************************
259 * Close: close the rtmp connection
260 *****************************************************************************/
261 static void Close( vlc_object_t * p_this )
263 access_t *p_access = (access_t *) p_this;
264 access_sys_t *p_sys = p_access->p_sys;
267 /* p_sys->p_thread->b_die = true;*/
268 vlc_object_kill( p_sys->p_thread );
269 block_FifoWake( p_sys->p_thread->p_fifo_input );
271 vlc_thread_join( p_sys->p_thread );
273 vlc_cond_destroy( &p_sys->p_thread->wait );
274 vlc_mutex_destroy( &p_sys->p_thread->lock );
276 block_FifoRelease( p_sys->p_thread->p_fifo_input );
277 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
279 for( i = 0; i < 64; i++ ) /* RTMP_HEADER_STREAM_INDEX_MASK */
281 if( p_sys->p_thread->rtmp_headers_recv[i].body != NULL )
283 free( p_sys->p_thread->rtmp_headers_recv[i].body->body );
284 free( p_sys->p_thread->rtmp_headers_recv[i].body );
288 net_Close( p_sys->p_thread->fd );
290 var_Destroy( p_access, "rtmp-caching" );
292 vlc_UrlClean( &p_sys->p_thread->url );
293 free( p_sys->p_thread->psz_application );
294 free( p_sys->p_thread->psz_media );
296 vlc_object_detach( p_sys->p_thread );
297 vlc_object_release( p_sys->p_thread );
301 /*****************************************************************************
302 * Read: standard read on a file descriptor.
303 *****************************************************************************/
304 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
306 access_sys_t *p_sys = p_access->p_sys;
307 rtmp_packet_t *rtmp_packet;
314 while( i_len_tmp < i_len )
316 if( p_sys->p_thread->result_stop || p_access->info.b_eof || !vlc_object_alive (p_access) )
318 p_access->info.b_eof = true;
322 if( p_sys->read_packet )
324 if( !p_sys->p_thread->metadata_received )
326 /* Wait until enough data is received for extracting metadata */
327 if( block_FifoCount( p_sys->p_thread->p_fifo_input ) < 10 )
333 p_sys->flv_packet = flv_get_metadata( p_access );
335 p_sys->p_thread->metadata_received = 1;
339 p_sys->flv_packet = block_FifoGet( p_sys->p_thread->p_fifo_input );
340 if( p_sys->flv_packet == NULL )
341 continue; /* Forced wake-up */
344 if( p_sys->p_thread->first_media_packet )
346 p_sys->flv_packet = flv_insert_header( p_access, p_sys->flv_packet );
348 p_sys->p_thread->first_media_packet = 0;
351 if( i_len - i_len_tmp >= p_sys->flv_packet->i_buffer )
353 p_sys->read_packet = 1;
355 memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, p_sys->flv_packet->i_buffer );
356 block_FifoPut( p_sys->p_thread->p_empty_blocks, p_sys->flv_packet );
358 i_len_tmp += p_sys->flv_packet->i_buffer;
362 p_sys->read_packet = 0;
364 memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, i_len - i_len_tmp);
365 p_sys->flv_packet->i_buffer -= i_len - i_len_tmp;
366 memmove( p_sys->flv_packet->p_buffer, p_sys->flv_packet->p_buffer + i_len - i_len_tmp, p_sys->flv_packet->i_buffer );
368 i_len_tmp += i_len - i_len_tmp;
373 if( p_sys->p_thread->result_publish )
375 /* Send publish onStatus event only once */
376 p_sys->p_thread->result_publish = 0;
378 rtmp_packet = rtmp_build_publish_start( p_sys->p_thread );
380 tmp_buffer = rtmp_encode_packet( p_sys->p_thread, rtmp_packet );
382 i_ret = net_Write( p_sys->p_thread, p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
383 if( i_ret != rtmp_packet->length_encoded )
385 free( rtmp_packet->body->body );
386 free( rtmp_packet->body );
389 msg_Err( p_access, "failed send publish start" );
392 free( rtmp_packet->body->body );
393 free( rtmp_packet->body );
398 p_access->info.i_pos += i_len_tmp;
400 rtmp_packet = rtmp_build_bytes_read( p_sys->p_thread, p_access->info.i_pos );
402 tmp_buffer = rtmp_encode_packet( p_sys->p_thread, rtmp_packet );
404 i_ret = net_Write( p_sys->p_thread, p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
405 if( i_ret != rtmp_packet->length_encoded )
407 free( rtmp_packet->body->body );
408 free( rtmp_packet->body );
411 msg_Err( p_access, "failed send bytes read" );
414 free( rtmp_packet->body->body );
415 free( rtmp_packet->body );
423 /*****************************************************************************
424 * Seek: seek to a specific location in a file
425 *****************************************************************************/
426 static int Seek( access_t *p_access, int64_t i_pos )
428 VLC_UNUSED( p_access );
430 /*msg_Warn ( p_access, "Seek to %lld", i_pos);
431 switch( rtmp_seek( p_access, i_pos ) )
438 msg_Err( p_access, "You should not be here" );
445 /*****************************************************************************
447 *****************************************************************************/
448 static int Control( access_t *p_access, int i_query, va_list args )
457 case ACCESS_CAN_SEEK:
458 case ACCESS_CAN_FASTSEEK:
459 pb_bool = (bool*) va_arg( args, bool* );
460 *pb_bool = false; /* TODO */
463 case ACCESS_CAN_PAUSE:
464 pb_bool = (bool*) va_arg( args, bool* );
465 *pb_bool = false; /* TODO */
468 case ACCESS_CAN_CONTROL_PACE:
469 pb_bool = (bool*) va_arg( args, bool* );
475 pi_int = (int*) va_arg( args, int * );
479 case ACCESS_GET_PTS_DELAY:
480 pi_64 = (int64_t*) va_arg( args, int64_t * );
481 *pi_64 = var_GetInteger( p_access, "rtmp-caching" ) * INT64_C(1000);
485 case ACCESS_SET_PAUSE_STATE:
489 case ACCESS_GET_TITLE_INFO:
490 case ACCESS_SET_TITLE:
491 case ACCESS_SET_SEEKPOINT:
492 case ACCESS_SET_PRIVATE_ID_STATE:
493 case ACCESS_GET_META:
494 case ACCESS_GET_CONTENT_TYPE: /* DOWN: comment this line */
498 msg_Warn( p_access, "unimplemented query in control" );
505 /*****************************************************************************
506 * ThreadControl: manage control messages and pipe media to Read
507 *****************************************************************************/
508 static void* ThreadControl( vlc_object_t *p_this )
510 rtmp_control_thread_t *p_thread = (rtmp_control_thread_t *) p_this;
511 rtmp_packet_t *rtmp_packet;
512 int canc = vlc_savecancel ();
514 rtmp_init_handler( p_thread->rtmp_handler );
516 while( vlc_object_alive (p_thread) )
518 rtmp_packet = rtmp_read_net_packet( p_thread );
519 if( rtmp_packet != NULL )
521 if( rtmp_packet->content_type < 0x01 /* RTMP_CONTENT_TYPE_CHUNK_SIZE */
522 || rtmp_packet->content_type > 0x14 ) /* RTMP_CONTENT_TYPE_INVOKE */
524 free( rtmp_packet->body->body );
525 free( rtmp_packet->body );
528 msg_Warn( p_thread, "unknown content type received" );
531 p_thread->rtmp_handler[rtmp_packet->content_type]( p_thread, rtmp_packet );
535 /* Sometimes server close connection too soon */
536 if( p_thread->result_connect )
538 vlc_mutex_lock( &p_thread->lock );
539 vlc_cond_signal( &p_thread->wait );
540 vlc_mutex_unlock( &p_thread->lock );
544 ((access_t *) p_thread->p_base_object)->info.b_eof = true;
546 block_FifoWake( p_thread->p_fifo_input );
549 vlc_restorecancel (canc);