📄 live555.cpp
字号:
/***************************************************************************** * live555.cpp : LIVE555 Streaming Media support. ***************************************************************************** * Copyright (C) 2003-2006 the VideoLAN team * $Id: live555.cpp 19578 2007-03-31 23:38:35Z hartman $ * * Authors: Laurent Aimar <fenrir@via.ecp.fr> * * 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> /* malloc(), free() */#include <vlc/vlc.h>#include <vlc/input.h>#include "network.h"#include <iostream>#if defined( WIN32 )# include <winsock2.h>#endif#include "BasicUsageEnvironment.hh"#include "GroupsockHelper.hh"#include "liveMedia.hh"extern "C" {#include "../access/mms/asf.h" /* Who said ugly ? */}#if (LIVEMEDIA_LIBRARY_VERSION_INT < 1089936000)#define RECLAIM_ENV(env) delete (env)#else#define RECLAIM_ENV(env) (env)->reclaim()#endifusing namespace std;/***************************************************************************** * Module descriptor *****************************************************************************/static int Open ( vlc_object_t * );static void Close( vlc_object_t * );#define CACHING_TEXT N_("Caching value (ms)")#define CACHING_LONGTEXT N_( \ "Allows you to modify the default caching value for RTSP streams. This " \ "value should be set in millisecond units." )#define KASENNA_TEXT N_( "Kasenna RTSP dialect")#define KASENNA_LONGTEXT N_( "Kasenna servers use an old and unstandard " \ "dialect of RTSP. When you set this parameter, VLC will try this dialect "\ "for communication. In this mode you cannot connect to normal RTSP servers." )#define USER_TEXT N_("RTSP user name")#define USER_LONGTEXT N_("Allows you to modify the user name that will " \ "be used for authenticating the connection.")#define PASS_TEXT N_("RTSP password")#define PASS_LONGTEXT N_("Allows you to modify the password that will be " \ "used for the connection.")vlc_module_begin(); set_description( _("RTP/RTSP/SDP demuxer (using Live555)" ) ); set_capability( "demux2", 50 ); set_shortname( "RTP/RTSP"); set_callbacks( Open, Close ); add_shortcut( "live" ); add_shortcut( "livedotcom" ); set_category( CAT_INPUT ); set_subcategory( SUBCAT_INPUT_DEMUX ); add_submodule(); set_description( _("RTSP/RTP access and demux") ); add_shortcut( "rtsp" ); add_shortcut( "sdp" ); set_capability( "access_demux", 0 ); set_callbacks( Open, Close ); add_bool( "rtsp-tcp", 0, NULL, N_("Use RTP over RTSP (TCP)"), N_("Use RTP over RTSP (TCP)"), VLC_TRUE ); add_integer( "rtp-client-port", -1, NULL, N_("Client port"), N_("Port to use for the RTP source of the session"), VLC_TRUE );#if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500 add_bool( "rtsp-http", 0, NULL, N_("Tunnel RTSP and RTP over HTTP"), N_("Tunnel RTSP and RTP over HTTP"), VLC_TRUE ); add_integer( "rtsp-http-port", 80, NULL, N_("HTTP tunnel port"), N_("Port to use for tunneling the RTSP/RTP over HTTP."), VLC_TRUE );#endif add_integer("rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE ); add_bool( "rtsp-kasenna", VLC_FALSE, NULL, KASENNA_TEXT, KASENNA_LONGTEXT, VLC_TRUE ); add_string( "rtsp-user", NULL, NULL, USER_TEXT, USER_LONGTEXT, VLC_TRUE ); add_string( "rtsp-pwd", NULL, NULL, PASS_TEXT, PASS_LONGTEXT, VLC_TRUE );vlc_module_end();/***************************************************************************** * Local prototypes *****************************************************************************/typedef struct{ demux_t *p_demux; vlc_bool_t b_quicktime; vlc_bool_t b_muxed; vlc_bool_t b_asf; es_format_t fmt; es_out_id_t *p_es; stream_t *p_out_muxed; /* for muxed stream */ RTPSource *rtpSource; FramedSource *readSource; vlc_bool_t b_rtcp_sync; uint8_t *p_buffer; unsigned int i_buffer; char waiting; int64_t i_pts;} live_track_t;struct timeout_thread_t{ VLC_COMMON_MEMBERS int64_t i_remain; demux_sys_t *p_sys;};struct demux_sys_t{ char *p_sdp; /* XXX mallocated */ char *psz_path; /* URL-encoded path */ MediaSession *ms; TaskScheduler *scheduler; UsageEnvironment *env ; RTSPClient *rtsp; /* */ int i_track; live_track_t **track; /* XXX mallocated */ int64_t i_pcr; int64_t i_pcr_start; int64_t i_pcr_previous; int64_t i_pcr_repeatdate; int i_pcr_repeats; /* Asf */ asf_header_t asfh; stream_t *p_out_asf; /* */ int64_t i_length; int64_t i_start; /* timeout thread information */ int i_timeout; /* session timeout value in seconds */ vlc_bool_t b_timeout_call;/* mark to send an RTSP call to prevent server timeout */ timeout_thread_t *p_timeout; /* the actual thread that makes sure we don't timeout */ /* */ vlc_bool_t b_multicast; /* true if one of the tracks is multicasted */ vlc_bool_t b_no_data; /* true if we never receive any data */ int i_no_data_ti; /* consecutive number of TaskInterrupt */ char event;};static int Demux ( demux_t * );static int Control( demux_t *, int, va_list );static int Connect ( demux_t * );static int SessionsSetup( demux_t * );static int Play ( demux_t *);static int ParseASF ( demux_t * );static int RollOverTcp ( demux_t * );static void StreamRead ( void *, unsigned int, unsigned int, struct timeval, unsigned int );static void StreamClose ( void * );static void TaskInterrupt( void * );static void TimeoutPrevention( timeout_thread_t * );#if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800static unsigned char* parseH264ConfigStr( char const* configStr, unsigned int& configSize );#endif/***************************************************************************** * DemuxOpen: *****************************************************************************/static int Open ( vlc_object_t *p_this ){ demux_t *p_demux = (demux_t*)p_this; demux_sys_t *p_sys = NULL; MediaSubsessionIterator *iter = NULL; MediaSubsession *sub = NULL; int i_return; if( p_demux->s ) { /* See if it looks like a SDP v, o, s fields are mandatory and in this order */ uint8_t *p_peek; if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC; if( memcmp( (char*)p_peek, "v=0\r\n", 5 ) && memcmp( (char*)p_peek, "v=0\n", 4 ) && ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) ) { return VLC_EGENERIC; } } else { var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT ); } p_demux->pf_demux = Demux; p_demux->pf_control= Control; p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) ); if( !p_sys ) return VLC_ENOMEM; p_sys->p_sdp = NULL; p_sys->scheduler = NULL; p_sys->env = NULL; p_sys->ms = NULL; p_sys->rtsp = NULL; p_sys->i_track = 0; p_sys->track = NULL; p_sys->i_pcr = 0; p_sys->i_pcr_start = 0; p_sys->i_pcr_previous = 0; p_sys->i_pcr_repeatdate = 0; p_sys->i_pcr_repeats = 0; p_sys->i_length = 0; p_sys->i_start = 0; p_sys->p_out_asf = NULL; p_sys->b_no_data = VLC_TRUE; p_sys->i_no_data_ti = 0; p_sys->p_timeout = NULL; p_sys->i_timeout = 0; p_sys->b_timeout_call = VLC_FALSE; p_sys->b_multicast = VLC_FALSE; p_sys->psz_path = strdup( p_demux->psz_path ); if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL ) { msg_Err( p_demux, "BasicTaskScheduler::createNew failed" ); goto error; } if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) ) { msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" ); goto error; } if( strcasecmp( p_demux->psz_access, "sdp" ) ) { char *p = p_sys->psz_path; while( (p = strchr( p, ' ' )) != NULL ) *p = '+'; } if( p_demux->s != NULL ) { /* Gather the complete sdp file */ int i_sdp = 0; int i_sdp_max = 1000; uint8_t *p_sdp = (uint8_t*) malloc( i_sdp_max ); for( ;; ) { int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp], i_sdp_max - i_sdp - 1 ); if( i_read < 0 ) { msg_Err( p_demux, "failed to read SDP" ); free( p_sys ); return VLC_EGENERIC; } i_sdp += i_read; if( i_read < i_sdp_max - i_sdp - 1 ) { p_sdp[i_sdp] = '\0'; break; } i_sdp_max += 1000; p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max ); } p_sys->p_sdp = (char*)p_sdp; } else if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "sdp" ) ) { /* sdp:// link from SAP */ p_sys->p_sdp = strdup( p_sys->psz_path ); } else if( ( i_return = Connect( p_demux ) ) != VLC_SUCCESS ) { msg_Err( p_demux, "Failed to connect with rtsp://%s", p_sys->psz_path ); goto error; } if( p_sys->p_sdp == NULL ) { msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" ); goto error; } /* Create the session from the SDP */ if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) ) { msg_Err( p_demux, "Could not create the RTSP Session: %s", p_sys->env->getResultMsg() ); goto error; } if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS ) { msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path ); goto error; } /* Retrieve the duration if possible */ p_sys->i_length = (int64_t)( p_sys->ms->playEndTime() * 1000000.0 ); if( p_sys->i_length < 0 ) p_sys->i_length = -1; if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS ) goto error; /* Create all es struct */ iter = new MediaSubsessionIterator( *p_sys->ms ); while( ( sub = iter->next() ) != NULL ) { live_track_t *tk; /* Check if we will receive data from this subsession for this track */ if( sub->readSource() == NULL ) continue; tk = (live_track_t*)malloc( sizeof( live_track_t ) ); tk->p_demux = p_demux; tk->waiting = 0; tk->i_pts = 0; tk->b_quicktime = VLC_FALSE; tk->b_muxed = VLC_FALSE; tk->b_asf = VLC_FALSE; tk->b_rtcp_sync = VLC_FALSE; tk->p_out_muxed = NULL; tk->p_es = NULL; tk->i_buffer = 65536; tk->p_buffer = (uint8_t *)malloc( 65536 ); /* Value taken from mplayer */ if( !strcmp( sub->mediumName(), "audio" ) ) { es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') ); tk->fmt.audio.i_channels = sub->numChannels(); tk->fmt.audio.i_rate = sub->rtpTimestampFrequency(); if( !strcmp( sub->codecName(), "MPA" ) || !strcmp( sub->codecName(), "MPA-ROBUST" ) || !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) ) { tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' ); tk->fmt.audio.i_rate = 0; } else if( !strcmp( sub->codecName(), "AC3" ) ) { tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' ); tk->fmt.audio.i_rate = 0; } else if( !strcmp( sub->codecName(), "L16" ) ) { tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' ); tk->fmt.audio.i_bitspersample = 16; } else if( !strcmp( sub->codecName(), "L8" ) ) { tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' ); tk->fmt.audio.i_bitspersample = 8; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -