📄 sap.c
字号:
/***************************************************************************** * sap.c : SAP interface module ***************************************************************************** * Copyright (C) 2004-2005 the VideoLAN team * $Id: sap.c 19775 2007-04-12 17:30:41Z courmisch $ * * Authors: Clément 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************//***************************************************************************** * Includes *****************************************************************************/#include <stdlib.h> /* malloc(), free() */#include <vlc/vlc.h>#include <vlc/input.h>#include <vlc/intf.h>#include <network.h>#include <charset.h>#include <ctype.h>#include <errno.h>#ifdef HAVE_UNISTD_H# include <unistd.h>#endif#ifdef HAVE_SYS_TIME_H# include <sys/time.h>#endif#ifdef HAVE_ZLIB_H# include <zlib.h>#endif/************************************************************************ * Macros and definitions ************************************************************************/#define MAX_LINE_LENGTH 256/* SAP is always on that port */#define SAP_PORT 9875/* Global-scope SAP address */#define SAP_V4_GLOBAL_ADDRESS "224.2.127.254"/* Organization-local SAP address */#define SAP_V4_ORG_ADDRESS "239.195.255.255"/* Local (smallest non-link-local scope) SAP address */#define SAP_V4_LOCAL_ADDRESS "239.255.255.255"/* Link-local SAP address */#define SAP_V4_LINK_ADDRESS "224.0.0.255"#define ADD_SESSION 1#define SAP_V6_1 "FF0"/* Scope is inserted between them */#define SAP_V6_2 "::2:7FFE"/* See RFC3513 for list of valid scopes *//* FIXME: find a way to listen to link-local scope */static const char ipv6_scopes[] = "1456789ABCDE";/***************************************************************************** * Module descriptor *****************************************************************************/#define SAP_ADDR_TEXT N_( "SAP multicast address" )#define SAP_ADDR_LONGTEXT N_( "The SAP module normally chooses itself the " \ "right addresses to listen to. However, you " \ "can specify a specific address." )#define SAP_IPV4_TEXT N_( "IPv4 SAP" )#define SAP_IPV4_LONGTEXT N_( \ "Listen to IPv4 announcements " \ "on the standard address." )#define SAP_IPV6_TEXT N_( "IPv6 SAP" )#define SAP_IPV6_LONGTEXT N_( \ "Listen to IPv6 announcements " \ "on the standard addresses." )#define SAP_SCOPE_TEXT N_( "IPv6 SAP scope" )#define SAP_SCOPE_LONGTEXT N_( \ "Scope for IPv6 announcements (default is 8)." )#define SAP_TIMEOUT_TEXT N_( "SAP timeout (seconds)" )#define SAP_TIMEOUT_LONGTEXT N_( \ "Delay after which SAP items get deleted if no new announcement " \ "is received." )#define SAP_PARSE_TEXT N_( "Try to parse the announce" )#define SAP_PARSE_LONGTEXT N_( \ "This enables actual parsing of the announces by the SAP module. " \ "Otherwise, all announcements are parsed by the \"livedotcom\" " \ "(RTP/RTSP) module." )#define SAP_STRICT_TEXT N_( "SAP Strict mode" )#define SAP_STRICT_LONGTEXT N_( \ "When this is set, the SAP parser will discard some non-compliant " \ "announcements." )#define SAP_CACHE_TEXT N_("Use SAP cache")#define SAP_CACHE_LONGTEXT N_( \ "This enables a SAP caching mechanism. " \ "This will result in lower SAP startup time, but you could end up " \ "with items corresponding to legacy streams." )#define SAP_TIMESHIFT_TEXT N_("Allow timeshifting")#define SAP_TIMESHIFT_LONGTEXT N_( "This automatically enables timeshifting " \ "for streams discovered through SAP announcements." )/* Callbacks */ static int Open ( vlc_object_t * ); static void Close( vlc_object_t * ); static int OpenDemux ( vlc_object_t * ); static void CloseDemux ( vlc_object_t * );vlc_module_begin(); set_shortname( _("SAP")); set_description( _("SAP Announcements") ); set_category( CAT_PLAYLIST ); set_subcategory( SUBCAT_PLAYLIST_SD ); add_string( "sap-addr", NULL, NULL, SAP_ADDR_TEXT, SAP_ADDR_LONGTEXT, VLC_TRUE ); add_bool( "sap-ipv4", 1 , NULL, SAP_IPV4_TEXT,SAP_IPV4_LONGTEXT, VLC_TRUE ); add_bool( "sap-ipv6", 1 , NULL, SAP_IPV6_TEXT, SAP_IPV6_LONGTEXT, VLC_TRUE ); add_integer( "sap-timeout", 1800, NULL, SAP_TIMEOUT_TEXT, SAP_TIMEOUT_LONGTEXT, VLC_TRUE ); add_bool( "sap-parse", 1 , NULL, SAP_PARSE_TEXT,SAP_PARSE_LONGTEXT, VLC_TRUE ); add_bool( "sap-strict", 0 , NULL, SAP_STRICT_TEXT,SAP_STRICT_LONGTEXT, VLC_TRUE );#if 0 add_bool( "sap-cache", 0 , NULL, SAP_CACHE_TEXT,SAP_CACHE_LONGTEXT, VLC_TRUE );#endif add_bool( "sap-timeshift", 0 , NULL, SAP_TIMESHIFT_TEXT,SAP_TIMESHIFT_LONGTEXT, VLC_TRUE ); set_capability( "services_discovery", 0 ); set_callbacks( Open, Close ); add_submodule(); set_description( _("SDP file parser for UDP") ); add_shortcut( "sdp" ); set_capability( "demux2", 51 ); set_callbacks( OpenDemux, CloseDemux );vlc_module_end();/***************************************************************************** * Local structures *****************************************************************************/typedef struct sdp_t sdp_t;typedef struct attribute_t attribute_t;typedef struct sap_announce_t sap_announce_t;/* The structure that contains sdp information */struct sdp_t{ char *psz_sdp; /* s= field */ char *psz_sessionname; /* Raw m= and c= fields */ char *psz_connection; char *psz_media; /* o field */ char *psz_username; char *psz_network_type; char *psz_address_type; char *psz_address; int64_t i_session_id; /* "computed" URI */ char *psz_uri; int i_in; /* IP version */ int i_media; int i_media_type; int i_attributes; attribute_t **pp_attributes;};struct attribute_t{ char *psz_field; char *psz_value;};struct sap_announce_t{ mtime_t i_last; uint16_t i_hash; uint32_t i_source[4]; /* SAP annnounces must only contain one SDP */ sdp_t *p_sdp; int i_item_id;// playlist_item_t *p_item;};struct services_discovery_sys_t{ /* Socket descriptors */ int i_fd; int *pi_fd; /* playlist node */ playlist_item_t *p_node; playlist_t *p_playlist; /* Table of announces */ int i_announces; struct sap_announce_t **pp_announces; /* Modes */ vlc_bool_t b_strict; vlc_bool_t b_parse; vlc_bool_t b_timeshift; int i_timeout;};struct demux_sys_t{ sdp_t *p_sdp;};/***************************************************************************** * Local prototypes *****************************************************************************//* Main functions */ static int Demux( demux_t *p_demux ); static int Control( demux_t *, int, va_list ); static void Run ( services_discovery_t *p_sd );/* Main parsing functions */ static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp ); static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read ); static sdp_t * ParseSDP( vlc_object_t *p_sd, char* psz_sdp ); static sap_announce_t *CreateAnnounce( services_discovery_t *, uint16_t, sdp_t * ); static int RemoveAnnounce( services_discovery_t *p_sd, sap_announce_t *p_announce );/* Helper functions */ static char *GetAttribute( sdp_t *p_sdp, const char *psz_search ); static vlc_bool_t IsSameSession( sdp_t *p_sdp1, sdp_t *p_sdp2 ); static int InitSocket( services_discovery_t *p_sd, const char *psz_address, int i_port );#ifdef HAVE_ZLIB_H static int Decompress( unsigned char *psz_src, unsigned char **_dst, int i_len );#endif static void FreeSDP( sdp_t *p_sdp );#define FREE( p ) \ if( p ) { free( p ); (p) = NULL; }/***************************************************************************** * Open: initialize and create stuff *****************************************************************************/static int Open( vlc_object_t *p_this ){ services_discovery_t *p_sd = ( services_discovery_t* )p_this; services_discovery_sys_t *p_sys = (services_discovery_sys_t *) malloc( sizeof( services_discovery_sys_t ) ); playlist_view_t *p_view; vlc_value_t val; p_sys->i_timeout = var_CreateGetInteger( p_sd, "sap-timeout" ); p_sd->pf_run = Run; p_sd->p_sys = p_sys; p_sys->pi_fd = NULL; p_sys->i_fd = 0; p_sys->b_strict = var_CreateGetInteger( p_sd, "sap-strict"); p_sys->b_parse = var_CreateGetInteger( p_sd, "sap-parse" );#if 0 if( var_CreateGetInteger( p_sd, "sap-cache" ) ) { CacheLoad( p_sd ); }#endif /* Cache sap_timeshift value */ p_sys->b_timeshift = var_CreateGetInteger( p_sd, "sap-timeshift" ) ? VLC_TRUE : VLC_FALSE; /* Create our playlist node */ p_sys->p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE ); if( !p_sys->p_playlist ) { msg_Warn( p_sd, "unable to find playlist, cancelling SAP listening"); return VLC_EGENERIC; } p_view = playlist_ViewFind( p_sys->p_playlist, VIEW_CATEGORY ); p_sys->p_node = playlist_NodeCreate( p_sys->p_playlist, VIEW_CATEGORY, _("Session Announcements (SAP)"), p_view->p_root ); p_sys->p_node->i_flags |= PLAYLIST_RO_FLAG; p_sys->p_node->i_flags &= ~PLAYLIST_SKIP_FLAG; val.b_bool = VLC_TRUE; var_Set( p_sys->p_playlist, "intf-change", val ); p_sys->i_announces = 0; p_sys->pp_announces = NULL; return VLC_SUCCESS;}/***************************************************************************** * OpenDemux: initialize and create stuff *****************************************************************************/static int OpenDemux( vlc_object_t *p_this ){ demux_t *p_demux = (demux_t *)p_this; uint8_t *p_peek; int i_max_sdp = 1024; int i_sdp = 0; char *psz_sdp = NULL; sdp_t *p_sdp = NULL; if( !var_CreateGetInteger( p_demux, "sap-parse" ) ) { /* We want livedotcom module to parse this SDP file */ return VLC_EGENERIC; } /* Probe for SDP */ if( p_demux->s ) { if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC; if( strncmp( (char*)p_peek, "v=0\r\n", 5 ) && strncmp( (char*)p_peek, "v=0\n", 4 ) && ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) ) { return VLC_EGENERIC; } } psz_sdp = (char *)malloc( i_max_sdp ); if( !psz_sdp ) return VLC_EGENERIC; /* Gather the complete sdp file */ for( ;; ) { int i_read = stream_Read( p_demux->s, &psz_sdp[i_sdp], i_max_sdp - i_sdp - 1 ); if( i_read < 0 ) { msg_Err( p_demux, "failed to read SDP" ); goto error; } i_sdp += i_read; if( i_read < i_max_sdp - i_sdp - 1 ) { psz_sdp[i_sdp] = '\0'; break; } i_max_sdp += 1000; psz_sdp = (char *)realloc( psz_sdp, i_max_sdp ); } p_sdp = ParseSDP( VLC_OBJECT(p_demux), psz_sdp ); if( !p_sdp ) { msg_Warn( p_demux, "invalid SDP"); goto error; } if( p_sdp->i_media > 1 ) { goto error; } if( ParseConnection( VLC_OBJECT( p_demux ), p_sdp ) ) { p_sdp->psz_uri = NULL; } if( p_sdp->i_media_type != 33 && p_sdp->i_media_type != 32 && p_sdp->i_media_type != 14 ) goto error; if( p_sdp->psz_uri == NULL ) goto error; p_demux->p_sys = (demux_sys_t *)malloc( sizeof(demux_sys_t) ); p_demux->p_sys->p_sdp = p_sdp; p_demux->pf_control = Control; p_demux->pf_demux = Demux; FREE( psz_sdp ); return VLC_SUCCESS;error: FREE( psz_sdp ); if( p_sdp ) FreeSDP( p_sdp ); p_sdp = NULL; stream_Seek( p_demux->s, 0 ); return VLC_EGENERIC;}/***************************************************************************** * Close: *****************************************************************************/static void Close( vlc_object_t *p_this ){ services_discovery_t *p_sd = ( services_discovery_t* )p_this; services_discovery_sys_t *p_sys = p_sd->p_sys; int i; for( i = p_sys->i_fd-1 ; i >= 0 ; i-- ) { net_Close( p_sys->pi_fd[i] ); } FREE( p_sys->pi_fd );#if 0 if( config_GetInt( p_sd, "sap-cache" ) ) { CacheSave( p_sd ); }#endif for( i = p_sys->i_announces - 1; i>= 0; i-- ) { RemoveAnnounce( p_sd, p_sys->pp_announces[i] ); } FREE( p_sys->pp_announces ); if( p_sys->p_playlist ) { playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node, VLC_TRUE, VLC_TRUE ); vlc_object_release( p_sys->p_playlist ); } free( p_sys );}/***************************************************************************** * CloseDemux: Close the demuxer *****************************************************************************/static void CloseDemux( vlc_object_t *p_this ){ demux_t *p_demux = (demux_t *)p_this; if( p_demux->p_sys ) { if( p_demux->p_sys->p_sdp ) { FreeSDP( p_demux->p_sys->p_sdp ); p_demux->p_sys->p_sdp = NULL; } free( p_demux->p_sys ); }}/***************************************************************************** * Run: main SAP thread ***************************************************************************** * Listens to SAP packets, and sends them to packet_handle
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -