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, true );
59 set_capability( "access", 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, 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;
281 msg_Warn(p_access, "Close");
283 vlc_object_kill( p_sys->p_thread );
284 block_FifoWake( p_sys->p_thread->p_fifo_media );
285 block_FifoWake( p_sys->p_thread->p_empty_blocks );
287 for( i = 0; i < 5; i++ )
289 block_t *p_dummy = block_New( p_access, 256 );
292 p_dummy->i_length = 0;
293 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
294 block_FifoPut( p_sys->p_thread->p_fifo_media, p_dummy );
296 for( i = 0; i < 5; i++ )
298 block_t *p_dummy = block_New( p_access, 256 );
301 p_dummy->i_length = 0;
302 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
303 block_FifoPut( p_sys->p_thread->p_empty_blocks, p_dummy );
305 vlc_thread_join( p_sys->p_thread );
307 vlc_cond_destroy( &p_sys->p_thread->wait );
308 vlc_mutex_destroy( &p_sys->p_thread->lock );
310 block_FifoRelease( p_sys->p_thread->p_fifo_media );
311 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
313 net_Close( p_sys->fd );
315 var_Destroy( p_access, "rtmp-caching" );
318 vlc_UrlClean( &p_sys->url );
319 free( p_sys->psz_application );
320 free( p_sys->psz_media );
324 /*****************************************************************************
325 * Read: standard read on a file descriptor.
326 *****************************************************************************/
327 static int Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
329 access_sys_t *p_sys = p_access->p_sys;
334 p_access->info.b_eof = true;
340 while( i_len_tmp < i_len )
342 if( p_sys->read_packet )
344 if( !p_sys->p_thread->metadata_received )
346 p_sys->flv_packet = flv_get_metadata( p_access );
348 p_sys->p_thread->metadata_received = 1;
352 if( p_sys->active && block_FifoCount( p_sys->p_thread->p_fifo_media ) == 0 )
354 p_access->info.b_eof = true;
358 p_sys->flv_packet = block_FifoGet( p_sys->p_thread->p_fifo_media );
359 if( p_sys->flv_packet == NULL )
360 continue; /* Forced wake-up */
363 if( p_sys->p_thread->first_media_packet )
365 p_sys->flv_packet = flv_insert_header( p_access, p_sys->flv_packet );
367 p_sys->p_thread->first_media_packet = 0;
370 if( i_len - i_len_tmp >= p_sys->flv_packet->i_buffer )
372 p_sys->read_packet = 1;
374 memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, p_sys->flv_packet->i_buffer );
375 block_FifoPut( p_sys->p_thread->p_empty_blocks, p_sys->flv_packet );
377 i_len_tmp += p_sys->flv_packet->i_buffer;
381 p_sys->read_packet = 0;
383 memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, i_len - i_len_tmp);
384 p_sys->flv_packet->i_buffer -= i_len - i_len_tmp;
385 memmove( p_sys->flv_packet->p_buffer, p_sys->flv_packet->p_buffer + i_len - i_len_tmp, p_sys->flv_packet->i_buffer );
387 i_len_tmp += i_len - i_len_tmp;
391 for(i = 0; i < i_len_tmp; i += 16)
393 msg_Warn(p_access,"%.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x",
394 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],
395 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]);
397 if( i_len_tmp > 0 ) {
398 if( p_sys->p_thread->result_publish )
400 /* Send publish onStatus event only once */
401 p_sys->p_thread->result_publish = 0;
403 rtmp_send_publish_start( p_access );
406 p_access->info.i_pos += i_len_tmp;
408 rtmp_send_bytes_read( p_access, p_access->info.i_pos );
414 /*****************************************************************************
415 * Seek: seek to a specific location in a file
416 *****************************************************************************/
417 static int Seek( access_t *p_access, int64_t i_pos )
419 /*msg_Warn ( p_access, "Seek to %lld", i_pos);
420 switch( rtmp_seek( p_access, i_pos ) )
427 msg_Err( p_access, "You should not be here" );
434 /*****************************************************************************
436 *****************************************************************************/
437 static int Control( access_t *p_access, int i_query, va_list args )
446 case ACCESS_CAN_SEEK:
447 case ACCESS_CAN_FASTSEEK:
448 pb_bool = (bool*)va_arg( args, bool* );
449 *pb_bool = false; /* TODO */
452 case ACCESS_CAN_PAUSE:
453 pb_bool = (bool*)va_arg( args, bool* );
454 *pb_bool = false; /* TODO */
457 case ACCESS_CAN_CONTROL_PACE:
458 pb_bool = (bool*)va_arg( args, bool* );
464 pi_int = (int*)va_arg( args, int * );
468 case ACCESS_GET_PTS_DELAY:
469 pi_64 = (int64_t*)va_arg( args, int64_t * );
470 *pi_64 = var_GetInteger( p_access, "rtmp-caching" ) * I64C(1000);
474 case ACCESS_SET_PAUSE_STATE:
478 case ACCESS_GET_TITLE_INFO:
479 case ACCESS_SET_TITLE:
480 case ACCESS_SET_SEEKPOINT:
481 case ACCESS_SET_PRIVATE_ID_STATE:
482 case ACCESS_GET_META:
483 case ACCESS_GET_CONTENT_TYPE: /* DOWN: comment this line */
487 msg_Warn( p_access, "unimplemented query in control" );
494 /*****************************************************************************
495 * ThreadControl: manage control messages and pipe media to Read
496 *****************************************************************************/
497 static void ThreadControl( vlc_object_t *p_this )
499 rtmp_control_thread_t *p_thread = (rtmp_control_thread_t *) p_this;
500 rtmp_packet_t *rtmp_packet;
502 rtmp_init_handler( p_thread->rtmp_handler );
504 while( !p_thread->b_die )
507 rtmp_packet = rtmp_read_net_packet( p_thread );
508 if( rtmp_packet != NULL )
510 if( rtmp_packet->content_type < 0x01 /* RTMP_CONTENT_TYPE_CHUNK_SIZE */
511 || rtmp_packet->content_type > 0x14 ) /* RTMP_CONTENT_TYPE_INVOKE */
512 msg_Warn( p_thread, "unknown content type received" );
514 p_thread->rtmp_handler[rtmp_packet->content_type]( p_thread, rtmp_packet );
518 /* Sometimes server close connection too soon */
519 if( p_thread->result_connect )
521 vlc_mutex_lock( &p_thread->lock );
522 vlc_cond_signal( &p_thread->wait );
523 vlc_mutex_unlock( &p_thread->lock );