📄 udp.c
字号:
/***************************************************************************** * udp.c ***************************************************************************** * Copyright (C) 2001-2007 the VideoLAN team * $Id: 2cadddf90bbf178a0362174671d64083553164cd $ * * Authors: Laurent Aimar <fenrir@via.ecp.fr> * Eric Petit <titer@videolan.org> * * 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 <sys/types.h>#include <sys/stat.h>#include <errno.h>#include <fcntl.h>#include <assert.h>#include <vlc_sout.h>#include <vlc_block.h>#ifdef HAVE_UNISTD_H# include <unistd.h>#endif#ifdef WIN32# include <winsock2.h># include <ws2tcpip.h>#else# include <sys/socket.h>#endif#include <vlc_network.h>#define MAX_EMPTY_BLOCKS 200/***************************************************************************** * Module descriptor *****************************************************************************/static int Open ( vlc_object_t * );static void Close( vlc_object_t * );#define SOUT_CFG_PREFIX "sout-udp-"#define CACHING_TEXT N_("Caching value (ms)")#define CACHING_LONGTEXT N_( \ "Default caching value for outbound UDP streams. This " \ "value should be set in milliseconds." )#define GROUP_TEXT N_("Group packets")#define GROUP_LONGTEXT N_("Packets can be sent one by one at the right time " \ "or by groups. You can choose the number " \ "of packets that will be sent at a time. It " \ "helps reducing the scheduling load on " \ "heavily-loaded systems." )vlc_module_begin(); set_description( N_("UDP stream output") ); set_shortname( "UDP" ); set_category( CAT_SOUT ); set_subcategory( SUBCAT_SOUT_ACO ); add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true ); add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT, true ); add_obsolete_integer( SOUT_CFG_PREFIX "late" ); add_obsolete_bool( SOUT_CFG_PREFIX "raw" ); set_capability( "sout access", 100 ); add_shortcut( "udp" ); set_callbacks( Open, Close );vlc_module_end();/***************************************************************************** * Exported prototypes *****************************************************************************/static const char *const ppsz_sout_options[] = { "caching", "group", NULL};/* Options handled by the libvlc network core */static const char *const ppsz_core_options[] = { "dscp", "ttl", "miface", "miface-addr", NULL};static ssize_t Write ( sout_access_out_t *, block_t * );static int Seek ( sout_access_out_t *, off_t );static void* ThreadWrite( vlc_object_t * );static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );typedef struct sout_access_thread_t{ VLC_COMMON_MEMBERS sout_instance_t *p_sout; block_fifo_t *p_fifo; int i_handle; int64_t i_caching; int i_group; block_fifo_t *p_empty_blocks;} sout_access_thread_t;struct sout_access_out_sys_t{ int i_mtu; bool b_mtu_warning; block_t *p_buffer; sout_access_thread_t *p_thread;};#define DEFAULT_PORT 1234/***************************************************************************** * Open: open the file *****************************************************************************/static int Open( vlc_object_t *p_this ){ sout_access_out_t *p_access = (sout_access_out_t*)p_this; sout_access_out_sys_t *p_sys; char *psz_dst_addr = NULL; int i_dst_port; int i_handle; config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg ); config_ChainParse( p_access, "", ppsz_core_options, p_access->p_cfg ); if (var_Create (p_access, "dst-port", VLC_VAR_INTEGER) || var_Create (p_access, "src-port", VLC_VAR_INTEGER) || var_Create (p_access, "dst-addr", VLC_VAR_STRING) || var_Create (p_access, "src-addr", VLC_VAR_STRING)) { return VLC_ENOMEM; } if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) ) return VLC_ENOMEM; p_access->p_sys = p_sys; i_dst_port = DEFAULT_PORT; char *psz_parser = psz_dst_addr = strdup( p_access->psz_path ); if( !psz_dst_addr ) { free( p_sys ); return VLC_ENOMEM; } if (psz_parser[0] == '[') psz_parser = strchr (psz_parser, ']'); psz_parser = strchr (psz_parser ?: psz_dst_addr, ':'); if (psz_parser != NULL) { *psz_parser++ = '\0'; i_dst_port = atoi (psz_parser); } p_sys->p_thread = vlc_object_create( p_access, sizeof( sout_access_thread_t ) ); if( !p_sys->p_thread ) { free (p_sys); free (psz_dst_addr); return VLC_ENOMEM; } vlc_object_attach( p_sys->p_thread, p_access ); p_sys->p_thread->p_sout = p_access->p_sout; p_sys->p_thread->b_die = 0; p_sys->p_thread->b_error= 0; p_sys->p_thread->p_fifo = block_FifoNew(); p_sys->p_thread->p_empty_blocks = block_FifoNew(); i_handle = net_ConnectDgram( p_this, psz_dst_addr, i_dst_port, -1, IPPROTO_UDP ); free (psz_dst_addr); if( i_handle == -1 ) { msg_Err( p_access, "failed to create raw UDP socket" ); vlc_object_release (p_sys->p_thread); free (p_sys); return VLC_EGENERIC; } else { char addr[NI_MAXNUMERICHOST]; int port; if (net_GetSockAddress (i_handle, addr, &port) == 0) { msg_Dbg (p_access, "source: %s port %d", addr, port); var_SetString (p_access, "src-addr", addr); var_SetInteger (p_access, "src-port", port); } if (net_GetPeerAddress (i_handle, addr, &port) == 0) { msg_Dbg (p_access, "destination: %s port %d", addr, port); var_SetString (p_access, "dst-addr", addr); var_SetInteger (p_access, "dst-port", port); } } p_sys->p_thread->i_handle = i_handle; shutdown( i_handle, SHUT_RD ); p_sys->p_thread->i_caching = (int64_t)1000 * var_GetInteger( p_access, SOUT_CFG_PREFIX "caching"); p_sys->p_thread->i_group = var_GetInteger( p_access, SOUT_CFG_PREFIX "group" ); p_sys->i_mtu = var_CreateGetInteger( p_this, "mtu" ); p_sys->p_buffer = NULL; if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite, VLC_THREAD_PRIORITY_HIGHEST, false ) ) { msg_Err( p_access->p_sout, "cannot spawn sout access thread" ); net_Close (i_handle); vlc_object_release( p_sys->p_thread ); free (p_sys); return VLC_EGENERIC; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -