📄 hxsm.cpp
字号:
/* ***** BEGIN LICENSE BLOCK *****
* Version: RCSL 1.0/RPSL 1.0
*
* Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
*
* The contents of this file, and the files included with this file, are
* subject to the current version of the RealNetworks Public Source License
* Version 1.0 (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the RealNetworks Community Source License Version 1.0
* (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
* in which case the RCSL will apply. You may also obtain the license terms
* directly from RealNetworks. You may not use this file except in
* compliance with the RPSL or, if you have a valid RCSL with RealNetworks
* applicable to this file, the RCSL. Please see the applicable RPSL or
* RCSL for the rights, obligations and limitations governing use of the
* contents of the file.
*
* This file is part of the Helix DNA Technology. RealNetworks is the
* developer of the Original Code and owns the copyrights in the portions
* it created.
*
* This file, and the files included with this file, is distributed and made
* available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
*
* Technology Compatibility Kit Test Suite(s) Location:
* http://www.helixcommunity.org/content/tck
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
/*
*
* ASM Manager for all streams in all Players.
*
* This class will manage the bandwidth requirements
* of multiple streams.
*/
#include "hlxclib/stdio.h" /* printf */
#include "hlxclib/stdlib.h" /* atoi on Mac */
#include "hxtypes.h" /* Basic Types */
#include "hxcom.h" /* IUnknown */
#include "hxslist.h"
#include "hxerror.h"
#include "hxsmbw.h"
#include "hxsm.h" /* HXSM */
#include "hxsmutil.h"
#include "hxpref.h"
#include "ihxpckts.h"
#include "hxcore.h"
#include "asmrulep.h"
#include "hxbuffer.h"
#include "chxpckts.h"
#include "hxbsrc.h"
#include "hxsrc.h"
#include "errdbg.h"
#include "rtspif.h"
#include "hxurl.h"
#include "hxtick.h"
#include "hxstrutl.h"
#include "hxbufctl.h"
#include "hxheap.h"
#ifdef _DEBUG
#undef HX_THIS_FILE
static const char HX_THIS_FILE[] = __FILE__;
#endif
#ifndef MIN
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif
#ifndef MAX
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#endif
HXSM::HXSM()
: m_State(HX_NONE)
, m_lRefCount(0)
, m_pSubscriptionVariables(0)
, m_ulOriginalHighestBandwidthAvail(0)
, m_ulHighestBandwidthAvail(0)
, m_ulPeakUsedBandwidth(0)
, m_ulUpShiftRingPos(0)
, m_ulUpShiftBandwidthAvail(0)
, m_ulNumSources(0)
, m_ulMaxAccelBitRate(0xffffffff)
, m_ulNumReportsSinceUpShift(2)
, m_ulLastStableBandwidth(0)
, m_ulUpShiftTestPointScaleFactor(4000)
, m_ulOfferToRecalc(0)
, m_ulNextPacketWindow(0)
, m_lPacketCounter(0)
, m_ulUpShiftPastResistanceCount(0)
, m_lLoss(0)
, m_bInitialHighBwAvail(TRUE)
, m_bPipeFull(FALSE)
, m_bUpShiftInfoReady(FALSE)
, m_bDidOfferUpShiftToRecalc(FALSE)
, m_bLoadTest(FALSE)
, m_bDoAccel(TRUE)
, m_bDoDecel(TRUE)
, m_bDisableBothAccelDecel(FALSE)
, m_fAccelerationFactor(4.0)
, m_bEnableSDB(TRUE)
#ifndef GOLD
, m_pEM(0)
#endif
{
m_pASMSourceInfo = new CHXSimpleList;
m_pASMStreamInfo = new CHXSimpleList;
}
HXSM::~HXSM()
{
#ifndef GOLD
HX_RELEASE(m_pEM);
#endif
CHXSimpleList::Iterator i;
ASMSourceInfo* pASMSourceInfo;
ASMStreamInfo* pASMStreamInfo;
for (i = m_pASMSourceInfo->Begin(); i != m_pASMSourceInfo->End(); ++i)
{
pASMSourceInfo = (ASMSourceInfo*)(*i);
pASMSourceInfo->Release();
}
for (i = m_pASMStreamInfo->Begin(); i != m_pASMStreamInfo->End(); ++i)
{
pASMStreamInfo = (ASMStreamInfo*)(*i);
delete pASMStreamInfo;
}
delete m_pASMSourceInfo;
delete m_pASMStreamInfo;
HX_RELEASE(m_pSubscriptionVariables);
}
STDMETHODIMP_(UINT32)
HXSM::AddRef(void)
{
return InterlockedIncrement(&m_lRefCount);
}
STDMETHODIMP_(UINT32)
HXSM::Release(void)
{
if (InterlockedDecrement(&m_lRefCount) > 0)
{
return m_lRefCount;
}
delete this;
return 0;
}
STDMETHODIMP
HXSM::QueryInterface
(
REFIID interfaceID,
void** ppInterfaceObj
)
{
QInterfaceList qiList[] =
{
{ GET_IIDHANDLE(IID_IUnknown), (IUnknown*)this },
{ GET_IIDHANDLE(IID_IHXBandwidthManager), (IHXBandwidthManager*)this },
};
return ::QIFind(qiList, QILISTSIZE(qiList), interfaceID, ppInterfaceObj);
}
STDMETHODIMP
HXSM::RegisterSource(HXSource* pSource, IUnknown* pUnknown)
{
IHXSourceBandwidthInfo* pSBI;
IHXPreferences* pPreferences = NULL;
ASMSourceInfo* pASMSourceInfo = NULL;
UINT16 i;
#ifndef GOLD
HX_RELEASE(m_pEM);
pUnknown->QueryInterface(IID_IHXErrorMessages, (void **)&m_pEM);
#endif
DEBUG_OUT(m_pEM, DOL_BWMGR, (s, "Register Source %p %s", pSource, pSource->GetURL()));
if (HXR_OK == pSource->QueryInterface(IID_IHXSourceBandwidthInfo,
(void **)&pSBI))
{
pASMSourceInfo = new ASMSourceInfo(pSource, this);
pASMSourceInfo->AddRef();
pASMSourceInfo->m_pSBI = pSBI;
/* Local source is always considered in perfect play mode UNLESS we are
* in simulated network playback mode (used Preview mode in the Encoder)
*/
pASMSourceInfo->m_bPerfectPlay = pSource->IsPerfectPlay() && !pSource->IsSimulatedNetworkPlayback();
m_ulNumSources++;
m_pASMSourceInfo->AddTail((void *)pASMSourceInfo);
pSBI->InitBw(pASMSourceInfo);
}
else
{
return HXR_OK;
}
/*
* This variable tells us the highest amount of bandwidth that we
* believe is useable. If we stream thin, then the highest value
* that the pipe can support would be the aggregate bandwidth of
* the sources. Barring any thinning, this would be the bandwidth
* from prefs (unless we can determine it through fast buffering or
* another transport feature). This value is used when starting sources
* as a initial guess for bandwidth (to set subscriptions).
*/
if (m_ulHighestBandwidthAvail == 0)
{
pUnknown->QueryInterface(IID_IHXPreferences, (void **)&pPreferences);
UINT32 ulTemp = 0;
/* Get initial bandwidth guess from Prefs */
if (HXR_OK == ReadPrefINT32(pPreferences, "Bandwidth", ulTemp))
{
m_bInitialHighBwAvail = TRUE;
/* Translate the bandwidth from prefs into a starting point */
if (ulTemp == 14400) m_ulHighestBandwidthAvail = 11000;
else if (ulTemp == 19200) m_ulHighestBandwidthAvail = 14400;
else if (ulTemp == 28800) m_ulHighestBandwidthAvail = 21600;
else if (ulTemp == 33600) m_ulHighestBandwidthAvail = 25000;
else if (ulTemp == 34400) m_ulHighestBandwidthAvail = 34400;
else if (ulTemp == 57600) m_ulHighestBandwidthAvail = 50000;
else if (ulTemp == 65536) m_ulHighestBandwidthAvail = 56360;
else if (ulTemp == 115200) m_ulHighestBandwidthAvail = 100000;
else if (ulTemp > 150000) m_ulHighestBandwidthAvail = (UINT32)(ulTemp * 0.90);
else
{
m_ulHighestBandwidthAvail = (UINT32)(ulTemp * 0.85);
}
/* Translate the bandwidth from prefs into a starting point */
if (ulTemp == 14400) m_ulPeakUsedBandwidth = 12240;
else if (ulTemp == 19200) m_ulPeakUsedBandwidth = 16320;
else if (ulTemp == 28800) m_ulPeakUsedBandwidth = 24480;
else if (ulTemp == 33600) m_ulPeakUsedBandwidth = 28560;
else if (ulTemp == 34400) m_ulPeakUsedBandwidth = 34400;
else if (ulTemp == 57600) m_ulPeakUsedBandwidth = 51840;
else if (ulTemp == 65536) m_ulPeakUsedBandwidth = 58980;
else if (ulTemp == 115200) m_ulPeakUsedBandwidth = 104000;
else if (ulTemp > 150000) m_ulPeakUsedBandwidth = (UINT32)(ulTemp * 0.91);
else
{
m_ulPeakUsedBandwidth = (UINT32)(ulTemp * 0.90);
}
/*
* Figure out the resistance bitrate for upshifting.
* Modems get 65k.
* DSL / Low BW LANs get their pref value.
* High bandwidth devices cap at 600k
* (unless the presentation is more)
*/
if (ulTemp < 65000)
{
m_ulResistanceBitRate = 65000;
}
else if (ulTemp < 600000)
{
m_ulResistanceBitRate = m_ulPeakUsedBandwidth;
}
else
{
m_ulResistanceBitRate = 600000;
}
m_ulOriginalResistanceBitRate = m_ulResistanceBitRate;
}
else
{
/* Wild Guess */
m_ulHighestBandwidthAvail = 40000;
m_ulPeakUsedBandwidth = 40000;
}
// LOAD TEST ONLY OPTIONS
// Read in the preferences for enabling/disabling Accel and
// Decel for load tests.
//
ReadPrefBOOL(pPreferences, "LoadTest", m_bLoadTest);
ReadPrefBOOL(pPreferences, "DoAccel", m_bDoAccel);
ReadPrefBOOL(pPreferences, "DoDecel", m_bDoDecel);
ReadPrefBOOL(pPreferences, "DisableBothAccelDecel", m_bDisableBothAccelDecel);
ReadPrefFLOAT(pPreferences, "AccelerationFactor", m_fAccelerationFactor);
// DisableBothAccelDecel override all other preferences.
// If it is true, set both DoAccel and DoDecel to false.
// If DisableBothAccelDecel is FALSE do nothing at all.
if (TRUE == m_bDisableBothAccelDecel)
{
m_bDoAccel = FALSE;
m_bDoDecel = FALSE;
}
/////////////// END LOAD TEST ONLY SECTION
/* Get MaxBandwidth from Prefs */
ReadPrefINT32(pPreferences, "MaxBandwidth", m_ulMaxAccelBitRate);
HX_RELEASE(pPreferences);
m_ulOriginalHighestBandwidthAvail = m_ulHighestBandwidthAvail;
}
/* This is ONLY used for load testing */
if (m_bLoadTest)
{
const char* pSourceURL = NULL;
CHXURL* pURL = NULL;
UINT32 ulTemp = 0;
IHXValues* pOptions = NULL;
pSourceURL = pSource->GetURL();
HX_ASSERT(pSourceURL);
pURL = new CHXURL(pSourceURL);
HX_ASSERT(pURL);
pOptions = pURL->GetOptions();
// Check for the DoAccel and DoDecel URL options. If they are
// there set the sources properties to reflect them. Also,
// each option is overriden, in the 'off' state by the global
// preferences DoAccel and DoDecel. Remember, both the
// preferences values can be made false by setting the global
// preference DisableBothAccelDecel to TRUE. Truth Table:
//
// Resulting ASM functionality = Option ^ Preference
//
// or,
//
// m_bSourceAccelAllowed = m_bSourceAccelAllowed^m_bDoAccel
// m_bSourceDecelAllowed = m_bSourceDecelAllowed^m_bDoDecel
//
// Resulting
// Preferences URL Option String ASM Functionality
// DoAccel DoDecel DoAccel DoDecel Accel Decel
// 0 0 0 0 0 0
// 0 0 0 1 0 0
// 0 0 1 0 0 0
// 0 0 1 1 0 0
// ... ... ... ... ... ...
// 0 1 0 0 0 0
// 1 1 1 1 1 1
// You get the idea. :-)
//First, get any option strings. They default to FALSE in
//the constructors.
if( NULL != pOptions )
{
if( pOptions->GetPropertyULONG32("DoAccel", ulTemp) == HXR_OK)
{
pASMSourceInfo->m_bSourceAccelAllowed = (ulTemp == 1);
}
if( pOptions->GetPropertyULONG32("DoDecel", ulTemp) == HXR_OK)
{
pASMSourceInfo->m_bSourceDecelAllowed = (ulTemp == 1);
}
}//NULL != pOptions
//Now, do the global preference overrides.
pASMSourceInfo->m_bSourceAccelAllowed =
pASMSourceInfo->m_bSourceAccelAllowed && m_bDoAccel;
pASMSourceInfo->m_bSourceDecelAllowed =
pASMSourceInfo->m_bSourceDecelAllowed && m_bDoDecel;
HX_RELEASE(pOptions);
HX_DELETE(pURL);
}//m_bLoadTest
//Report status of load test vars.
// DEBUG_OUT( m_pEM, DOL_ASM, (s, "LoadTest %d", m_bLoadTest));
// DEBUG_OUT( m_pEM, DOL_ASM, (s, " DoAccel %u", (UINT16)m_bDoAccel));
// DEBUG_OUT( m_pEM, DOL_ASM, (s, " DoDecel %u", (UINT16)m_bDoDecel));
// DEBUG_OUT( m_pEM, DOL_ASM, (s, " DisableBothAccelDecel %d",
// m_bDisableBothAccelDecel));
// DEBUG_OUT( m_pEM, DOL_ASM, (s, " m_bSourceAccelAllowed %d",
// pASMSourceInfo->m_bSourceAccelAllowed));
// DEBUG_OUT( m_pEM, DOL_ASM, (s, " m_bSourceDecelAllowed %d",
// pASMSourceInfo->m_bSourceDecelAllowed));
UINT32 unStreamCount = pSource->GetStreamCount();
pASMSourceInfo->m_pStreams = new ASMStreamInfo*[unStreamCount];;
BOOL bEnableSDB = FALSE;
bEnableSDB = (unStreamCount == 0);
pASMSourceInfo->m_ulLowestBandwidthBeforeTimeStamp = 0;
for (i = 0; i < unStreamCount; i++)
{
IUnknown* pStream = 0;
IHXStream* pHXStream = NULL;
ASMStreamInfo* pInfo = new ASMStreamInfo;
HX_VERIFY(HXR_OK == pSource->GetStream(i, pStream));
HX_VERIFY(HXR_OK == pStream->QueryInterface
(IID_IHXStreamBandwidthNegotiator,
(void **)&pInfo->m_pNegotiator));
HX_VERIFY(HXR_OK == pStream->QueryInterface
(IID_IHXStreamBandwidthBias,
(void **)&pInfo->m_pBias));
HX_VERIFY(HXR_OK == pStream->QueryInterface
(IID_IHXAtomicRuleGather,
(void **)&pInfo->m_pRuleGather));
pStream->QueryInterface(IID_IHXStream, (void**)&pHXStream);
if (pHXStream)
{
pInfo->m_ulStreamNumber = pHXStream->GetStreamNumber();
pHXStream->Release();
}
pInfo->m_pNegotiator->GetFixedBandwidth(pInfo->m_ulFixedBandwidth);
if (pInfo->m_ulFixedBandwidth != 1)
{
bEnableSDB = TRUE;
}
pInfo->m_pASMSourceInfo = pASMSourceInfo;
pASMSourceInfo->m_pStreams[i] = pInfo;
m_pASMStreamInfo->AddTail((void *)pInfo);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -