📄 sap.c
字号:
/***************************************************************************** * sap.c : SAP announce handler ***************************************************************************** * Copyright (C) 2002-2004 VideoLAN * $Id: sap.c 11232 2005-06-01 18:34:09Z courmisch $ * * Authors: Cl閙ent Stenac <zorglub@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., 59 Temple Place - Suite 330, Boston, MA 02111, USA. *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#include <stdlib.h> /* free() */#include <stdio.h> /* sprintf() */#include <string.h> /* strerror() */#include <vlc/vlc.h>#include <vlc/sout.h>#include <network.h>#include "charset.h"#define SAP_IPV4_ADDR "224.2.127.254" /* Standard port and address for SAP */#define SAP_PORT 9875#define SAP_IPV6_ADDR_1 "FF0"#define SAP_IPV6_ADDR_2 "::2:7FFE"#define DEFAULT_IPV6_SCOPE '8'#define DEFAULT_PORT "1234"#undef EXTRA_DEBUG/***************************************************************************** * Local prototypes *****************************************************************************/static void RunThread( vlc_object_t *p_this);static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address );static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session );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, announce_method_t *p_method );static int announce_SAPAnnounceDel( sap_handler_t *p_sap, session_descriptor_t *p_session );static char *convert_to_utf8( struct sap_handler_t *p_this, char *psz_local );#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; char *psz_charset; 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 ); vlc_current_charset( &psz_charset ); p_sap->iconvHandle = vlc_iconv_open( "UTF-8", psz_charset ); free( psz_charset ); if( p_sap->iconvHandle == (vlc_iconv_t)(-1) ) { msg_Warn( p_sap, "Unable to do requested conversion" ); } 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 ); } if( p_sap->iconvHandle != (vlc_iconv_t)(-1) ) vlc_iconv_close( p_sap->iconvHandle ); /* 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, announce_method_t *p_method ){ int i; char *psz_type = "application/sdp"; int i_header_size; char *psz_head; vlc_bool_t b_found = VLC_FALSE; sap_session_t *p_sap_session; mtime_t i_hash; vlc_mutex_lock( &p_sap->object_lock ); /* If needed, build the SDP */ if( !p_session->psz_sdp ) { if ( SDPGenerate( p_sap, p_session ) != VLC_SUCCESS ) { vlc_mutex_unlock( &p_sap->object_lock ); return VLC_EGENERIC; } } if( !p_method->psz_address ) { if( p_method->i_ip_version == 6 ) { char sz_scope; if( p_method->sz_ipv6_scope ) { sz_scope = p_method->sz_ipv6_scope; } else { sz_scope = DEFAULT_IPV6_SCOPE; } p_method->psz_address = (char*)malloc( 30*sizeof(char )); sprintf( p_method->psz_address, "%s%c%s", SAP_IPV6_ADDR_1, sz_scope, SAP_IPV6_ADDR_2 ); } else { /* IPv4 */ p_method->psz_address = (char*)malloc( 15*sizeof(char) ); snprintf(p_method->psz_address, 15, SAP_IPV4_ADDR ); } } msg_Dbg( p_sap, "using SAP address: %s",p_method->psz_address); /* XXX: Check for dupes */ p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t)); p_sap_session->psz_sdp = strdup( p_session->psz_sdp ); p_sap_session->i_last = 0; /* Add the address to the buffer */ for( i = 0; i< p_sap->i_addresses; i++) { if( !strcmp( p_method->psz_address, p_sap->pp_addresses[i]->psz_address ) ) { p_sap_session->p_address = p_sap->pp_addresses[i]; b_found = VLC_TRUE; break; } } if( b_found == VLC_FALSE ) { sap_address_t *p_address = (sap_address_t *) malloc( sizeof(sap_address_t) ); if( !p_address ) { msg_Err( p_sap, "out of memory" ); return VLC_ENOMEM; } p_address->psz_address = strdup( p_method->psz_address ); p_address->i_ip_version = p_method->i_ip_version; p_address->i_port = 9875; p_address->i_wfd = net_OpenUDP( p_sap, "", 0, p_address->psz_address, p_address->i_port ); if( p_sap->b_control == VLC_TRUE ) { p_address->i_rfd = net_OpenUDP( p_sap, p_method->psz_address, p_address->i_port, "", 0 ); p_address->i_buff = 0; p_address->b_enabled = VLC_TRUE; p_address->b_ready = VLC_FALSE; p_address->i_limit = 10000; /* 10000 bps */ p_address->t1 = 0; } else { p_address->b_enabled = VLC_TRUE; p_address->b_ready = VLC_TRUE; p_address->i_interval = config_GetInt( p_sap,"sap-interval"); p_address->i_rfd = -1; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -