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

📄 sap.c

📁 uclinux 下的vlc播放器源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************** * sap.c : SAP announce handler ***************************************************************************** * Copyright (C) 2002-2005 the VideoLAN team * $Id: sap.c 19051 2007-02-27 20:22:19Z courmisch $ * * Authors: Clément Stenac <zorglub@videolan.org> *          Rémi Denis-Courmont <rem # 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>                                                /* free() */#include <stdio.h>                                              /* sprintf() */#include <string.h>                                            /* strerror() */#include <ctype.h>                                  /* tolower(), isxdigit() */#include <vlc/vlc.h>#include <vlc/sout.h>#include "network.h"#include "charset.h"/* SAP is always on that port */#define SAP_PORT 9875#define DEFAULT_PORT "1234"#undef EXTRA_DEBUG/* SAP Specific structures *//* 100ms */#define SAP_IDLE ((mtime_t)(0.100*CLOCK_FREQ))#define SAP_MAX_BUFFER 65534#define MIN_INTERVAL 2#define MAX_INTERVAL 300/* A SAP announce address. For each of these, we run the * control flow algorithm */struct sap_address_t{    char *psz_address;    char psz_machine[NI_MAXNUMERICHOST];    int i_rfd; /* Read socket */    int i_wfd; /* Write socket */    /* Used for flow control */    mtime_t t1;    vlc_bool_t b_enabled;    vlc_bool_t b_ready;    int i_interval;    int i_buff;    int i_limit;};/***************************************************************************** * Local prototypes *****************************************************************************/static void RunThread( vlc_object_t *p_this);static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address );static char *SDPGenerate( sap_handler_t *p_sap,                          const session_descriptor_t *p_session,                          const sap_address_t *p_addr, vlc_bool_t b_ssm );static int announce_SendSAPAnnounce( sap_handler_t *p_sap,                                     sap_session_t *p_session );static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,                             session_descriptor_t *p_session );static int announce_SAPAnnounceDel( sap_handler_t *p_sap,                             session_descriptor_t *p_session );#define FREE( p ) if( p ) { free( p ); (p) = NULL; }/** * Create the SAP handler * * \param p_announce the parent announce_handler * \return the newly created SAP handler or NULL on error */sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce ){    sap_handler_t *p_sap;    p_sap = vlc_object_create( p_announce, sizeof( sap_handler_t ) );    if( !p_sap )    {        msg_Err( p_announce, "out of memory" );        return NULL;    }    vlc_mutex_init( p_sap, &p_sap->object_lock );    p_sap->pf_add = announce_SAPAnnounceAdd;    p_sap->pf_del = announce_SAPAnnounceDel;    p_sap->i_sessions = 0;    p_sap->i_addresses = 0;    p_sap->i_current_session = 0;    p_sap->b_control = config_GetInt( p_sap, "sap-flow-control");    if( vlc_thread_create( p_sap, "sap handler", RunThread,                       VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )    {        msg_Dbg( p_announce, "unable to spawn SAP handler thread");        free( p_sap );        return NULL;    };    msg_Dbg( p_announce, "thread created, %i sessions", p_sap->i_sessions);    return p_sap;}/** *  Destroy the SAP handler *  \param p_this the SAP Handler to destroy *  \return nothing */void announce_SAPHandlerDestroy( sap_handler_t *p_sap ){    int i;    vlc_mutex_destroy( &p_sap->object_lock );    /* Free the remaining sessions */    for( i = 0 ; i< p_sap->i_sessions ; i++)    {        sap_session_t *p_session = p_sap->pp_sessions[i];        FREE( p_session->psz_sdp );        FREE( p_session->psz_data );        REMOVE_ELEM( p_sap->pp_sessions, p_sap->i_sessions , i );        FREE( p_session );    }    /* Free the remaining addresses */    for( i = 0 ; i< p_sap->i_addresses ; i++)    {        sap_address_t *p_address = p_sap->pp_addresses[i];        FREE( p_address->psz_address );        if( p_address->i_rfd > -1 )        {            net_Close( p_address->i_rfd );        }        if( p_address->i_wfd > -1 && p_sap->b_control )        {            net_Close( p_address->i_wfd );        }        REMOVE_ELEM( p_sap->pp_addresses, p_sap->i_addresses, i );        FREE( p_address );    }    /* Free the structure */    vlc_object_destroy( p_sap );}/** * main SAP handler thread * \param p_this the SAP Handler object * \return nothing */static void RunThread( vlc_object_t *p_this){    sap_handler_t *p_sap = (sap_handler_t*)p_this;    sap_session_t *p_session;    while( !p_sap->b_die )    {        int i;        /* If needed, get the rate info */        if( p_sap->b_control == VLC_TRUE )        {            for( i = 0 ; i< p_sap->i_addresses ; i++)            {                if( p_sap->pp_addresses[i]->b_enabled == VLC_TRUE )                {                    CalculateRate( p_sap, p_sap->pp_addresses[i] );                }            }        }        /* Find the session to announce */        vlc_mutex_lock( &p_sap->object_lock );        if( p_sap->i_sessions > p_sap->i_current_session + 1)        {            p_sap->i_current_session++;        }        else if( p_sap->i_sessions > 0)        {            p_sap->i_current_session = 0;        }        else        {            vlc_mutex_unlock( &p_sap->object_lock );            msleep( SAP_IDLE );            continue;        }        p_session = p_sap->pp_sessions[p_sap->i_current_session];        vlc_mutex_unlock( &p_sap->object_lock );        /* And announce it */        if( p_session->p_address->b_enabled == VLC_TRUE &&            p_session->p_address->b_ready == VLC_TRUE )        {            announce_SendSAPAnnounce( p_sap, p_session );        }        msleep( SAP_IDLE );    }}/* Add a SAP announce */static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,                             session_descriptor_t *p_session ){    int i_header_size, i;    char *psz_head, psz_addr[NI_MAXNUMERICHOST];    vlc_bool_t b_ipv6 = VLC_FALSE, b_ssm = VLC_FALSE;    sap_session_t *p_sap_session;    mtime_t i_hash;    struct addrinfo hints, *res;    struct sockaddr_storage addr;    vlc_mutex_lock( &p_sap->object_lock );    if( p_session->psz_uri == NULL )    {        vlc_mutex_unlock( &p_sap->object_lock );        msg_Err( p_sap, "*FIXME* unexpected NULL URI for SAP announce" );        msg_Err( p_sap, "This should not happen. VLC needs fixing." );        return VLC_EGENERIC;    }    /* Determine SAP multicast address automatically */    memset( &hints, 0, sizeof( hints ) );    hints.ai_socktype = SOCK_DGRAM;    hints.ai_flags = AI_NUMERICHOST;    i = vlc_getaddrinfo( (vlc_object_t *)p_sap, p_session->psz_uri, 0,                         &hints, &res );    if( i )    {        vlc_mutex_unlock( &p_sap->object_lock );        msg_Err( p_sap, "Invalid URI for SAP announce: %s: %s",                 p_session->psz_uri, vlc_gai_strerror( i ) );        return VLC_EGENERIC;    }    if( (unsigned)res->ai_addrlen > sizeof( addr ) )    {        vlc_mutex_unlock( &p_sap->object_lock );        vlc_freeaddrinfo( res );        msg_Err( p_sap, "Unsupported address family of size %d > %u",                 res->ai_addrlen, (unsigned) sizeof( addr ) );        return VLC_EGENERIC;    }    memcpy( &addr, res->ai_addr, res->ai_addrlen );    switch( addr.ss_family )    {#if defined (HAVE_INET_PTON) || defined (WIN32)        case AF_INET6:        {            /* See RFC3513 for list of valid IPv6 scopes */            struct in6_addr *a6 = &((struct sockaddr_in6 *)&addr)->sin6_addr;            memcpy( a6->s6_addr + 2, "\x00\x00\x00\x00\x00\x00"                   "\x00\x00\x00\x00\x00\x02\x7f\xfe", 14 );            if( IN6_IS_ADDR_MULTICAST( a6 ) )            {                /* SSM <=> ff3x::/32 */                b_ssm = (U32_AT (a6->s6_addr) & 0xfff0ffff) == 0xff300000;                /* force flags to zero, preserve scope */                a6->s6_addr[1] &= 0xf;            }            else                /* Unicast IPv6 - assume global scope */                memcpy( a6->s6_addr, "\xff\x0e", 2 );            b_ipv6 = VLC_TRUE;            break;        }#endif        case AF_INET:        {            /* See RFC2365 for IPv4 scopes */            uint32_t ipv4;            ipv4 = ntohl( ((struct sockaddr_in *)&addr)->sin_addr.s_addr );            /* 224.0.0.0/24 => 224.0.0.255 */            if ((ipv4 & 0xffffff00) == 0xe0000000)                ipv4 =  0xe00000ff;            else            /* 239.255.0.0/16 => 239.255.255.255 */            if ((ipv4 & 0xffff0000) == 0xefff0000)                ipv4 =  0xefffffff;            else            /* 239.192.0.0/14 => 239.195.255.255 */            if ((ipv4 & 0xfffc0000) == 0xefc00000)                ipv4 =  0xefc3ffff;            else            if ((ipv4 & 0xff000000) == 0xef000000)                ipv4 = 0;            else            /* other addresses => 224.2.127.254 */            {                /* SSM: 232.0.0.0/8 */                b_ssm = (ipv4 >> 24) == 232;                ipv4 = 0xe0027ffe;            }            if( ipv4 == 0 )            {                msg_Err( p_sap, "Out-of-scope multicast address "                        "not supported by SAP: %s", p_session->psz_uri );                vlc_mutex_unlock( &p_sap->object_lock );                vlc_freeaddrinfo( res );                return VLC_EGENERIC;            }            ((struct sockaddr_in *)&addr)->sin_addr.s_addr = htonl( ipv4 );            break;        }        default:            vlc_mutex_unlock( &p_sap->object_lock );            vlc_freeaddrinfo( res );            msg_Err( p_sap, "Address family %d not supported by SAP",                     addr.ss_family );            return VLC_EGENERIC;    }    i = vlc_getnameinfo( (struct sockaddr *)&addr, res->ai_addrlen,                         psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );    vlc_freeaddrinfo( res );    if( i )    {        vlc_mutex_unlock( &p_sap->object_lock );

⌨️ 快捷键说明

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