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 *****************************************************************************/
31 #include <vlc_access.h>
33 #include <vlc_network.h> /* DOWN: #include <network.h> */
35 #include <vlc_block.h>
37 #include "rtmp_amf_flv.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 #define CACHING_TEXT N_("Caching value in ms")
43 #define CACHING_LONGTEXT N_( \
44 "Caching value for RTMP streams. This " \
45 "value should be set in milliseconds." )
47 static int Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
51 set_description( _("RTMP input") );
52 set_shortname( _("RTMP") );
53 set_category( CAT_INPUT );
54 set_subcategory( SUBCAT_INPUT_ACCESS );
56 add_integer( "rtmp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
57 CACHING_LONGTEXT, VLC_TRUE );
59 set_capability( "access2", 10 );
60 set_callbacks( Open, Close );
61 add_shortcut( "rtmp" );
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Read( access_t *, uint8_t *, size_t ); /*DOWN: last parameter int */
69 static int Seek( access_t *, int64_t );
70 static int Control( access_t *, int, va_list );
72 static void ThreadControl( vlc_object_t * );
74 /*****************************************************************************
75 * Open: open the rtmp connection
76 *****************************************************************************/
77 static int Open( vlc_object_t *p_this )
79 access_t *p_access = (access_t *) p_this;
82 int length_path, length_media_name;
85 STANDARD_READ_ACCESS_INIT
87 /* Parse URI - remove spaces */
88 p = psz = strdup( p_access->psz_path );
89 while( (p = strchr( p, ' ' )) != NULL )
91 vlc_UrlParse( &p_sys->url, psz, 0 );
94 if( !p_access->psz_access ||
95 strncmp( p_access->psz_access, "rtmp", 4 ))
97 msg_Warn( p_access, "invalid protocol" );
98 vlc_UrlClean( &p_sys->url );
103 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
105 msg_Warn( p_access, "invalid host" );
106 vlc_UrlClean( &p_sys->url );
111 if( p_sys->url.i_port <= 0 )
112 p_sys->url.i_port = 1935;
114 if ( p_sys->url.psz_path == NULL ) {
115 msg_Warn( p_access, "invalid path" );
116 vlc_UrlClean( &p_sys->url );
121 length_path = strlen( p_sys->url.psz_path );
122 length_media_name = strlen( strrchr( p_sys->url.psz_path, '/' ) ) - 1;
124 p_sys->psz_application = strndup( p_sys->url.psz_path + 1, length_path - length_media_name - 2 );
125 p_sys->psz_media = strdup( p_sys->url.psz_path + ( length_path - length_media_name ) );
127 msg_Dbg( p_access, "rtmp: host='%s' port=%d path='%s'",
128 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
130 if( p_sys->url.psz_username && *p_sys->url.psz_username )
132 msg_Dbg( p_access, " user='%s', pwd='%s'",
133 p_sys->url.psz_username, p_sys->url.psz_password );
137 vlc_object_create( p_access, sizeof( rtmp_control_thread_t ) );
138 if( !p_sys->p_thread )
140 msg_Err( p_access, "out of memory" );
141 vlc_UrlClean( &p_sys->url );
146 vlc_object_attach( p_sys->p_thread, p_access );
147 p_sys->p_thread->b_die = 0;
148 p_sys->p_thread->b_error= 0;
149 p_sys->p_thread->p_fifo_media = block_FifoNew( p_access );
150 p_sys->p_thread->p_empty_blocks = block_FifoNew( p_access );
151 p_sys->p_thread->has_audio = 0;
152 p_sys->p_thread->has_video = 0;
153 p_sys->p_thread->metadata_received = 0;
154 p_sys->p_thread->first_media_packet = 1;
155 p_sys->p_thread->flv_tag_previous_tag_size = 0x00000000; /* FLV_TAG_FIRST_PREVIOUS_TAG_SIZE */
156 for(i = 0; i < 64; i++)
158 memset( &p_sys->p_thread->rtmp_headers_recv[i], 0, sizeof( rtmp_packet_t ) );
159 p_sys->p_thread->rtmp_headers_send[i].length_header = -1;
160 p_sys->p_thread->rtmp_headers_send[i].stream_index = -1;
161 p_sys->p_thread->rtmp_headers_send[i].timestamp = -1;
162 p_sys->p_thread->rtmp_headers_send[i].timestamp_relative = -1;
163 p_sys->p_thread->rtmp_headers_send[i].length_encoded = -1;
164 p_sys->p_thread->rtmp_headers_send[i].length_body = -1;
165 p_sys->p_thread->rtmp_headers_send[i].content_type = -1;
166 p_sys->p_thread->rtmp_headers_send[i].src_dst = -1;
167 p_sys->p_thread->rtmp_headers_send[i].body = NULL;
170 vlc_cond_init( p_sys->p_thread, &p_sys->p_thread->wait );
171 vlc_mutex_init( p_sys->p_thread, &p_sys->p_thread->lock );
173 p_sys->p_thread->result_connect = 1;
174 p_sys->p_thread->result_play = 1;
176 /* Open connection */
177 p_sys->fd = net_ConnectTCP( p_access, p_sys->url.psz_host, p_sys->url.i_port );
178 p_sys->p_thread->fd = p_sys->fd;
179 if( p_sys->fd == -1 )
183 msg_Warn( p_access, "cannot connect to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
184 msg_Dbg( p_access, "switching to passive mode" );
188 p_fd_listen = net_ListenTCP( p_access, p_sys->url.psz_host, p_sys->url.i_port );
189 if( p_fd_listen == NULL )
191 msg_Warn( p_access, "cannot listen to %s port %i", p_sys->url.psz_host, p_sys->url.i_port );
192 vlc_UrlClean( &p_sys->url );
193 net_Close( p_sys-> fd );
198 p_sys->fd = net_Accept( p_access, p_fd_listen, -1 );
200 net_ListenClose( p_fd_listen );
202 if( rtmp_handshake_passive( p_this ) < 0 )
204 msg_Err( p_access, "Passive handshake failed");
205 vlc_UrlClean( &p_sys->url );
206 net_Close( p_sys-> fd );
211 p_sys->p_thread->result_publish = 1;
216 msg_Dbg( p_access, "using active connection");
219 if( rtmp_handshake_active( p_this ) < 0 )
221 msg_Err( p_access, "Active handshake failed");
222 vlc_UrlClean( &p_sys->url );
223 net_Close( p_sys-> fd );
228 p_sys->p_thread->result_publish = 0;
231 if( vlc_thread_create( p_sys->p_thread, "rtmp control thread", ThreadControl,
232 VLC_THREAD_PRIORITY_INPUT, VLC_FALSE ) )
234 msg_Err( p_access, "cannot spawn rtmp control thread" );
235 vlc_UrlClean( &p_sys->url );
236 net_Close( p_sys-> fd );
243 msg_Dbg( p_access, "Activation active connection");
244 if( rtmp_connect_active( p_this ) < 0)
246 msg_Err( p_access, "Active connection failed");
247 vlc_UrlClean( &p_sys->url );
248 net_Close( p_sys-> fd );
254 /* Set vars for reading from fifo */
255 p_access->p_sys->flv_packet = NULL;
256 p_access->p_sys->read_packet = 1;
258 msg_Dbg( p_access, "waiting for buffer to fill");
259 /* Wait until enough data is received for extracting metadata */
260 while( block_FifoCount( p_access->p_sys->p_thread->p_fifo_media ) < 10 )
262 msg_Dbg( p_access, "waiting for buffer to fill");
267 /* Update default_pts to a suitable value for rtmp access */
268 var_Create( p_access, "rtmp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
273 /*****************************************************************************
274 * Close: close the rtmp connection
275 *****************************************************************************/
276 static void Close( vlc_object_t * p_this )
278 access_t *p_access = (access_t *) p_this;
279 access_sys_t *p_sys = p_access->p_sys;
282 msg_Warn(p_access, "Close");
284 vlc_object_kill( p_sys->p_thread );
285 block_FifoWake( p_sys->p_thread->p_fifo_media );
286 block_FifoWake( p_sys->p_thread->p_empty_blocks );
288 for( i = 0; i < 5; i++ )
290 block_t *p_dummy = block_New( p_access, 256 );
293 p_dummy->i_length = 0;
294 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
295 block_FifoPut( p_sys->p_thread->p_fifo_media, p_dummy );
297 for( i = 0; i < 5; i++ )
299 block_t *p_dummy = block_New( p_access, 256 );
302 p_dummy->i_length = 0;
303 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
304 block_FifoPut( p_sys->p_thread->p_empty_blocks, p_dummy );
306 vlc_thread_join( p_sys->p_thread );
308 vlc_cond_destroy( &p_sys->p_thread->wait );
309 vlc_mutex_destroy( &p_sys->p_thread->lock );
311 block_FifoRelease( p_sys->p_thread->p_fifo_media );
312 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
314 net_Close( p_sys->fd );
316 var_Destroy( p_access, "rtmp-caching" );
319 vlc_UrlClean( &p_sys->url );
320 free( p_sys->psz_application );
321 free( p_sys->psz_media );
325 /*****************************************************************************
326 * Read: standard read on a file descriptor.
327 *****************************************************************************/
328 static int Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
330 access_sys_t *p_sys = p_access->p_sys;
335 p_access->info.b_eof = VLC_TRUE;
341 while( i_len_tmp < i_len )
343 if( p_sys->read_packet )
345 if( !p_sys->p_thread->metadata_received )
347 p_sys->flv_packet = flv_get_metadata( p_access );
349 p_sys->p_thread->metadata_received = 1;
353 if( p_sys->active && block_FifoCount( p_sys->p_thread->p_fifo_media ) == 0 )
355 p_access->info.b_eof = VLC_TRUE;
359 p_sys->flv_packet = block_FifoGet( p_sys->p_thread->p_fifo_media );
360 if( p_sys->flv_packet == NULL )
361 continue; /* Forced wake-up */
364 if( p_sys->p_thread->first_media_packet )
366 p_sys->flv_packet = flv_insert_header( p_access, p_sys->flv_packet );
368 p_sys->p_thread->first_media_packet = 0;
371 if( i_len - i_len_tmp >= p_sys->flv_packet->i_buffer )
373 p_sys->read_packet = 1;
375 memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, p_sys->flv_packet->i_buffer );
376 block_FifoPut( p_sys->p_thread->p_empty_blocks, p_sys->flv_packet );
378 i_len_tmp += p_sys->flv_packet->i_buffer;
382 p_sys->read_packet = 0;
384 memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, i_len - i_len_tmp);
385 p_sys->flv_packet->i_buffer -= i_len - i_len_tmp;
386 memmove( p_sys->flv_packet->p_buffer, p_sys->flv_packet->p_buffer + i_len - i_len_tmp, p_sys->flv_packet->i_buffer );
388 i_len_tmp += i_len - i_len_tmp;
392 for(i = 0; i < i_len_tmp; i += 16)
394 msg_Warn(p_access,"%.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x",
395 p_buffer[i], p_buffer[i+1], p_buffer[i+2], p_buffer[i+3], p_buffer[i+4], p_buffer[i+5], p_buffer[i+6], p_buffer[i+7],
396 p_buffer[i+8], p_buffer[i+9], p_buffer[i+10], p_buffer[i+11], p_buffer[i+12], p_buffer[i+13], p_buffer[i+14], p_buffer[i+15]);
398 if( i_len_tmp > 0 ) {
399 if( p_sys->p_thread->result_publish )
401 /* Send publish onStatus event only once */
402 p_sys->p_thread->result_publish = 0;
404 rtmp_send_publish_start( p_access );
407 p_access->info.i_pos += i_len_tmp;
409 rtmp_send_bytes_read( p_access, p_access->info.i_pos );
415 /*****************************************************************************
416 * Seek: seek to a specific location in a file
417 *****************************************************************************/
418 static int Seek( access_t *p_access, int64_t i_pos )
420 /*msg_Warn ( p_access, "Seek to %lld", i_pos);
421 switch( rtmp_seek( p_access, i_pos ) )
428 msg_Err( p_access, "You should not be here" );
435 /*****************************************************************************
437 *****************************************************************************/
438 static int Control( access_t *p_access, int i_query, va_list args )
447 case ACCESS_CAN_SEEK:
448 case ACCESS_CAN_FASTSEEK:
449 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
450 *pb_bool = VLC_FALSE; /* TODO */
453 case ACCESS_CAN_PAUSE:
454 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
455 *pb_bool = VLC_FALSE; /* TODO */
458 case ACCESS_CAN_CONTROL_PACE:
459 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
465 pi_int = (int*)va_arg( args, int * );
469 case ACCESS_GET_PTS_DELAY:
470 pi_64 = (int64_t*)va_arg( args, int64_t * );
471 *pi_64 = var_GetInteger( p_access, "rtmp-caching" ) * I64C(1000);
475 case ACCESS_SET_PAUSE_STATE:
479 case ACCESS_GET_TITLE_INFO:
480 case ACCESS_SET_TITLE:
481 case ACCESS_SET_SEEKPOINT:
482 case ACCESS_SET_PRIVATE_ID_STATE:
483 case ACCESS_GET_META:
484 case ACCESS_GET_CONTENT_TYPE: /* DOWN: comment this line */
488 msg_Warn( p_access, "unimplemented query in control" );
495 /*****************************************************************************
496 * ThreadControl: manage control messages and pipe media to Read
497 *****************************************************************************/
498 static void ThreadControl( vlc_object_t *p_this )
500 rtmp_control_thread_t *p_thread = (rtmp_control_thread_t *) p_this;
501 rtmp_packet_t *rtmp_packet;
503 rtmp_init_handler( p_thread->rtmp_handler );
505 while( !p_thread->b_die )
508 rtmp_packet = rtmp_read_net_packet( p_thread );
509 if( rtmp_packet != NULL )
511 if( rtmp_packet->content_type < 0x01 /* RTMP_CONTENT_TYPE_CHUNK_SIZE */
512 || rtmp_packet->content_type > 0x14 ) /* RTMP_CONTENT_TYPE_INVOKE */
513 msg_Warn( p_thread, "unknown content type received" );
515 p_thread->rtmp_handler[rtmp_packet->content_type]( p_thread, rtmp_packet );
519 /* Sometimes server close connection too soon */
520 if( p_thread->result_connect )
522 vlc_mutex_lock( &p_thread->lock );
523 vlc_cond_signal( &p_thread->wait );
524 vlc_mutex_unlock( &p_thread->lock );