⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udp.c

📁 uclinux 下的vlc播放器源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************** * udp.c ***************************************************************************** * Copyright (C) 2001-2005 the VideoLAN team * $Id: udp.c 19592 2007-04-01 00:31:04Z hartman $ * * 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 *****************************************************************************/#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <string.h>#include <errno.h>#include <fcntl.h>#include <vlc/vlc.h>#include <vlc/sout.h>#ifdef HAVE_UNISTD_H#   include <unistd.h>#endif#ifdef WIN32#   include <winsock2.h>#   include <ws2tcpip.h>#   ifndef IN_MULTICAST#       define IN_MULTICAST(a) IN_CLASSD(a)#   endif#else#   include <sys/socket.h>#endif#include "network.h"#define MAX_EMPTY_BLOCKS 200#if defined(WIN32) || defined(UNDER_CE)# define WINSOCK_STRERROR_SIZE 20static const char *winsock_strerror( char *buf ){    snprintf( buf, WINSOCK_STRERROR_SIZE, "Winsock error %d",              WSAGetLastError( ) );    buf[WINSOCK_STRERROR_SIZE - 1] = '\0';    return buf;}#endif/***************************************************************************** * 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 TTL_TEXT N_("Time-To-Live (TTL)")#define TTL_LONGTEXT N_("Time-To-Live of the " \                        "outgoing stream.")#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." )#define RAW_TEXT N_("Raw write")#define RAW_LONGTEXT N_("Packets will be sent " \                       "directly, without trying to fill the MTU (ie, " \                       "without trying to make the biggest possible packets " \                       "in order to improve streaming)." )vlc_module_begin();    set_description( _("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, VLC_TRUE );    add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL,TTL_TEXT, TTL_LONGTEXT,                                 VLC_TRUE );    add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,                                 VLC_TRUE );    add_suppressed_integer( SOUT_CFG_PREFIX "late" );    add_bool( SOUT_CFG_PREFIX "raw",  0, NULL, RAW_TEXT, RAW_LONGTEXT,                                 VLC_TRUE );    set_capability( "sout access", 100 );    add_shortcut( "udp" );    add_shortcut( "rtp" ); // Will work only with ts muxer    set_callbacks( Open, Close );vlc_module_end();/***************************************************************************** * Exported prototypes *****************************************************************************/static const char *ppsz_sout_options[] = {    "caching",    "ttl",    "group",    "raw",    NULL};static int  Write   ( sout_access_out_t *, block_t * );static int  WriteRaw( 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                 b_rtpts;  // 1 if add rtp/ts header    uint16_t            i_sequence_number;    uint32_t            i_ssrc;    int                 i_mtu;    block_t             *p_buffer;    sout_access_thread_t *p_thread;    vlc_bool_t          b_mtu_warning;};#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_parser;    char                *psz_dst_addr;    int                 i_dst_port;    int                 i_handle;    vlc_value_t         val;    sout_CfgParse( p_access, SOUT_CFG_PREFIX,                   ppsz_sout_options, p_access->p_cfg );    if( !( p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )    {        msg_Err( p_access, "not enough memory" );        return VLC_EGENERIC;    }    memset( p_sys, 0, sizeof(sout_access_out_sys_t) );    p_access->p_sys = p_sys;    if( p_access->psz_access != NULL &&        !strcmp( p_access->psz_access, "rtp" ) )    {        p_sys->b_rtpts = 1;    }    else    {        p_sys->b_rtpts = 0;    }    psz_parser = strdup( p_access->psz_name );    psz_dst_addr = psz_parser;    i_dst_port = 0;    if ( *psz_parser == '[' )    {        while( *psz_parser && *psz_parser != ']' )        {            psz_parser++;        }    }    while( *psz_parser && *psz_parser != ':' )    {        psz_parser++;    }    if( *psz_parser == ':' )    {        *psz_parser = '\0';        psz_parser++;        i_dst_port = atoi( psz_parser );    }    if( i_dst_port <= 0 )    {        i_dst_port = DEFAULT_PORT;    }    p_sys->p_thread =        vlc_object_create( p_access, sizeof( sout_access_thread_t ) );    if( !p_sys->p_thread )    {        msg_Err( p_access, "out of memory" );        return VLC_EGENERIC;    }    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_access );    p_sys->p_thread->p_empty_blocks = block_FifoNew( p_access );    var_Get( p_access, SOUT_CFG_PREFIX "ttl", &val );    i_handle = net_ConnectUDP( p_this, psz_dst_addr, i_dst_port, val.i_int );    if( i_handle == -1 )    {         msg_Err( p_access, "failed to open a connection (udp)" );         return VLC_EGENERIC;    }    p_sys->p_thread->i_handle = i_handle;    net_StopRecv( i_handle );    var_Get( p_access, SOUT_CFG_PREFIX "caching", &val );    p_sys->p_thread->i_caching = (int64_t)val.i_int * 1000;    var_Get( p_access, SOUT_CFG_PREFIX "group", &val );    p_sys->p_thread->i_group = val.i_int;    p_sys->i_mtu = var_CreateGetInteger( p_this, "mtu" );    if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,                           VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )    {        msg_Err( p_access->p_sout, "cannot spawn sout access thread" );        vlc_object_destroy( p_sys->p_thread );        return VLC_EGENERIC;    }    srand( (uint32_t)mdate());    p_sys->p_buffer          = NULL;    p_sys->i_sequence_number = rand()&0xffff;    p_sys->i_ssrc            = rand()&0xffffffff;    var_Get( p_access, SOUT_CFG_PREFIX "raw", &val );    if( val.b_bool )  p_access->pf_write = WriteRaw;    else p_access->pf_write = Write;    p_access->pf_seek = Seek;    msg_Dbg( p_access, "udp access output opened(%s:%d)",             psz_dst_addr, i_dst_port );    free( psz_dst_addr );    /* update p_sout->i_out_pace_nocontrol */    p_access->p_sout->i_out_pace_nocontrol++;    return VLC_SUCCESS;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -