📄 access.c
字号:
/***************************************************************************** * access.c: RTMP input. ***************************************************************************** * Copyright (C) URJC - LADyR - Luis Lopez Fernandez * * Author: Miguel Angel Cabrera Moya * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#ifdef HAVE_CONFIG_H# include "config.h"#endif#include <vlc_common.h>#include <vlc_plugin.h>#include <vlc_access.h>#include <vlc_network.h> /* DOWN: #include <network.h> */#include <vlc_url.h>#include <vlc_block.h>#include "rtmp_amf_flv.h"/***************************************************************************** * Module descriptor *****************************************************************************/#define CACHING_TEXT N_("Caching value in ms")#define CACHING_LONGTEXT N_( \ "Caching value for RTMP streams. This " \ "value should be set in milliseconds." )static int Open ( vlc_object_t * );static void Close( vlc_object_t * );vlc_module_begin(); set_description( N_("RTMP input") ); set_shortname( N_("RTMP") ); set_category( CAT_INPUT ); set_subcategory( SUBCAT_INPUT_ACCESS ); add_integer( "rtmp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true ); set_capability( "access", 0 ); set_callbacks( Open, Close ); add_shortcut( "rtmp" );vlc_module_end();/***************************************************************************** * Local prototypes *****************************************************************************/static ssize_t Read( access_t *, uint8_t *, size_t );static int Seek( access_t *, int64_t );static int Control( access_t *, int, va_list );static void* ThreadControl( vlc_object_t * );/***************************************************************************** * Open: open the rtmp connection *****************************************************************************/static int Open( vlc_object_t *p_this ){ access_t *p_access = (access_t *) p_this; access_sys_t *p_sys; char *psz, *p; int length_path, length_media_name; int i; STANDARD_READ_ACCESS_INIT p_sys->p_thread = vlc_object_create( p_access, sizeof( rtmp_control_thread_t ) ); if( !p_sys->p_thread ) return VLC_ENOMEM; vlc_object_attach( p_sys->p_thread, p_access ); /* Parse URI - remove spaces */ p = psz = strdup( p_access->psz_path ); while( (p = strchr( p, ' ' )) != NULL ) *p = '+'; vlc_UrlParse( &p_sys->p_thread->url, psz, 0 ); free( psz ); if( p_sys->p_thread->url.psz_host == NULL || *p_sys->p_thread->url.psz_host == '\0' ) { msg_Warn( p_access, "invalid host" ); goto error; } if( p_sys->p_thread->url.i_port <= 0 ) p_sys->p_thread->url.i_port = 1935; if( p_sys->p_thread->url.psz_path == NULL ) { msg_Warn( p_access, "invalid path" ); goto error; } length_path = strlen( p_sys->p_thread->url.psz_path ); length_media_name = strlen( strrchr( p_sys->p_thread->url.psz_path, '/' ) ) - 1; p_sys->p_thread->psz_application = strndup( p_sys->p_thread->url.psz_path + 1, length_path - length_media_name - 2 ); p_sys->p_thread->psz_media = strdup( p_sys->p_thread->url.psz_path + ( length_path - length_media_name ) ); msg_Dbg( p_access, "rtmp: host='%s' port=%d path='%s'", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port, p_sys->p_thread->url.psz_path ); if( p_sys->p_thread->url.psz_username && *p_sys->p_thread->url.psz_username ) { msg_Dbg( p_access, " user='%s', pwd='%s'", p_sys->p_thread->url.psz_username, p_sys->p_thread->url.psz_password ); } /* Initialize thread variables */ p_sys->p_thread->b_die = 0; p_sys->p_thread->b_error= 0; p_sys->p_thread->p_fifo_input = block_FifoNew(); p_sys->p_thread->p_empty_blocks = block_FifoNew(); p_sys->p_thread->has_audio = 0; p_sys->p_thread->has_video = 0; p_sys->p_thread->metadata_received = 0; p_sys->p_thread->first_media_packet = 1; p_sys->p_thread->flv_tag_previous_tag_size = 0x00000000; /* FLV_TAG_FIRST_PREVIOUS_TAG_SIZE */ p_sys->p_thread->chunk_size_recv = 128; /* RTMP_DEFAULT_CHUNK_SIZE */ p_sys->p_thread->chunk_size_send = 128; /* RTMP_DEFAULT_CHUNK_SIZE */ for(i = 0; i < 64; i++) { memset( &p_sys->p_thread->rtmp_headers_recv[i], 0, sizeof( rtmp_packet_t ) ); p_sys->p_thread->rtmp_headers_send[i].length_header = -1; p_sys->p_thread->rtmp_headers_send[i].stream_index = -1; p_sys->p_thread->rtmp_headers_send[i].timestamp = -1; p_sys->p_thread->rtmp_headers_send[i].timestamp_relative = -1; p_sys->p_thread->rtmp_headers_send[i].length_encoded = -1; p_sys->p_thread->rtmp_headers_send[i].length_body = -1; p_sys->p_thread->rtmp_headers_send[i].content_type = -1; p_sys->p_thread->rtmp_headers_send[i].src_dst = -1; p_sys->p_thread->rtmp_headers_send[i].body = NULL; } p_sys->p_thread->p_base_object = p_this; vlc_cond_init( p_sys->p_thread, &p_sys->p_thread->wait ); vlc_mutex_init( &p_sys->p_thread->lock ); p_sys->p_thread->result_connect = 1; p_sys->p_thread->result_play = 1; p_sys->p_thread->result_stop = 0; /* Open connection */ p_sys->p_thread->fd = net_ConnectTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port ); if( p_sys->p_thread->fd == -1 ) { int *p_fd_listen; msg_Warn( p_access, "cannot connect to %s:%d", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port ); msg_Dbg( p_access, "switching to passive mode" ); p_sys->active = 0; p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port ); if( p_fd_listen == NULL ) { msg_Err( p_access, "cannot listen to %s port %i", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port ); goto error2; } p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen, -1 ); net_ListenClose( p_fd_listen ); if( rtmp_handshake_passive( p_this, p_sys->p_thread->fd ) < 0 ) { msg_Err( p_access, "handshake passive failed"); goto error2; } p_sys->p_thread->result_publish = 1; } else { p_sys->active = 1; if( rtmp_handshake_active( p_this, p_sys->p_thread->fd ) < 0 ) { msg_Err( p_access, "handshake active failed"); goto error2; } p_sys->p_thread->result_publish = 0; } if( vlc_thread_create( p_sys->p_thread, "rtmp control thread", ThreadControl, VLC_THREAD_PRIORITY_INPUT, false ) ) { msg_Err( p_access, "cannot spawn rtmp control thread" ); goto error2; } if( p_sys->active ) { if( rtmp_connect_active( p_sys->p_thread ) < 0 ) { msg_Err( p_access, "connect active failed"); goto error2; } } /* Set vars for reading from fifo */ p_access->p_sys->flv_packet = NULL; p_access->p_sys->read_packet = 1; /* Update default_pts to a suitable value for rtmp access */ var_Create( p_access, "rtmp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT ); return VLC_SUCCESS;error2: vlc_cond_destroy( &p_sys->p_thread->wait ); vlc_mutex_destroy( &p_sys->p_thread->lock ); free( p_sys->p_thread->psz_application ); free( p_sys->p_thread->psz_media ); net_Close( p_sys->p_thread->fd );error: vlc_UrlClean( &p_sys->p_thread->url ); vlc_object_detach( p_sys->p_thread ); vlc_object_release( p_sys->p_thread ); free( p_sys ); return VLC_EGENERIC;}/***************************************************************************** * Close: close the rtmp connection *****************************************************************************/static void Close( vlc_object_t * p_this ){ access_t *p_access = (access_t *) p_this; access_sys_t *p_sys = p_access->p_sys; int i;/* p_sys->p_thread->b_die = true;*/ vlc_object_kill( p_sys->p_thread ); block_FifoWake( p_sys->p_thread->p_fifo_input ); block_FifoWake( p_sys->p_thread->p_empty_blocks ); vlc_thread_join( p_sys->p_thread ); vlc_cond_destroy( &p_sys->p_thread->wait ); vlc_mutex_destroy( &p_sys->p_thread->lock ); block_FifoRelease( p_sys->p_thread->p_fifo_input );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -