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

📄 hxsm2.cpp

📁 著名的 helix realplayer 基于手机 symbian 系统的 播放器全套源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* ***** 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 ***** */ 

#include "hxsm2.h"
#include "hxsmutil.h"
#include "ihxpckts.h"
#include "hxprefs.h"
#include "hxpref.h"
#include "hxbsrc.h" /* HXSource */
#include "asmrulep.h" /* ASMRuleBook */
#include "hxbuffer.h" /* CHXBuffer */
#include "chxpckts.h" /* CHXHeader */
#include "safestring.h" /* SafeSprintf */
#include "rtspif.h"     /* RTSPSubscription */

class ASMStreamInfo2;

class ASMSourceInfo2
{
public:
    ASMSourceInfo2(HXSource* pSource);
    ~ASMSourceInfo2();

    UINT32 StreamCount() const;
    ASMStreamInfo2* GetStream(UINT32 i);

    UINT32& SubscribeBw();
    UINT32& LastSetDelivery();

    UINT32& MasterOffer();

    BOOL IsMySource(HXSource* pSource);
    BOOL IsLive();
    BOOL IsPerfectPlay() const;

    BOOL HasMasterRuleBook() const;
    void DistributeBw();

    CHXSimpleList* SubsChanges();

    void SubscribeToChanges();
    void SetDeliveryBw(UINT32 ulMaxBw);

    void Done();

private:
    HX_RESULT GetSubInfo(UINT32 ulBandwidth,
			 BOOL* pCurrentSubInfo);

    UINT32 GetStreamBw(IHXValues* pProps, 
		       UINT32 ulStreamIndex) const;

    HXSource* m_pSource;

    UINT32 m_ulStreamCount;
    ASMStreamInfo2** m_pStreams;

    UINT32 m_ulSubscribedBw;
    UINT32 m_ulLastSetDelivery;

    UINT32 m_ulMasterOffer;

    BOOL m_bPerfectPlay;
    ASMRuleBook* m_pMasterRuleBook;
    IHXValues* m_pSubsVars;
    CHXSimpleList   m_subsChanges;
};

class ASMStreamInfo2
{
public:
    ASMStreamInfo2(IUnknown* pStream);
    ~ASMStreamInfo2();

    HX_RESULT GetBiasFactor(REF(INT32) lBiasFactor);

    BOOL IsFixedBw() const;
    UINT32 CurrentBw() const;

    UINT32 StreamID() const;
    
    void SetStreamOffer(UINT32 ulOffer);

    void SelectBw(BOOL bPerfectPlay,
		  UINT32 ulAltOffer);
    
    UINT32 ResistanceToLower() const;

    void SelectNextLowerBw();

    UINT32 GetLastBw() const;
    void SubscribeToNewBw(UINT32 ulBw, CHXSimpleList* pSubChanges);
    void SubscribeToChanges(CHXSimpleList* pSubChanges);

private:
    void ComputeResistence(BOOL bPerfectPlay);

    BOOL   m_bFixedBwValid;
    UINT32 m_ulFixedBandwidth;
    UINT32 m_ulNumThresholds;
    float* m_pThresholds;
    UINT32 m_ulCurThresh;
    UINT32 m_ulMaxEffectiveThreshold;
    IHXStreamBandwidthBias* m_pBias;
    IHXStreamBandwidthNegotiator* m_pNegotiator;
    IHXAtomicRuleGather* m_pRuleGather;

    UINT32 m_ulStreamID;

    UINT32 m_ulStreamOffer; // From master rulebook
    UINT32 m_ulOffer;       // From SelectBw()
    UINT32 m_ulResistanceToLower;

    UINT32 m_ulLastBw;
};

class ASMStreamInfoItr
{
public:
    ASMStreamInfoItr(CHXSimpleList* pSourceList);
    
    ASMSourceInfo2* Source();
    ASMStreamInfo2* Stream();

    BOOL More() const;
    void Next();
    void Reset();

private:
    void FindNextSource();

    CHXSimpleList* m_pSourceList;
    CHXSimpleList::Iterator m_itr;
    UINT32         m_ulStream;
};

ASMStreamInfoItr::ASMStreamInfoItr(CHXSimpleList* pSourceList) :
    m_pSourceList(pSourceList),
    m_ulStream(0)
{
    Reset();
}

ASMSourceInfo2* ASMStreamInfoItr::Source()
{
    ASMSourceInfo2* pRet = 0;
    
    if (More())
    {
	pRet = (ASMSourceInfo2*)(*m_itr);
    }

    return pRet;
}

ASMStreamInfo2* ASMStreamInfoItr::Stream()
{
    ASMStreamInfo2* pRet = 0;
    
    ASMSourceInfo2* pSrc = Source();

    if (pSrc)
    {
	pRet = pSrc->GetStream(m_ulStream);
    }

    return pRet;
}

BOOL ASMStreamInfoItr::More() const
{
    return  (m_pSourceList && 
	     (m_itr != m_pSourceList->End()));
}

void ASMStreamInfoItr::Next()
{
    if (More())
    {
	m_ulStream++;

	if (m_ulStream == Source()->StreamCount())
	{
	    // We are done with this source.
	    // Move onto the next one
	    m_ulStream = 0;

	    ++m_itr;
	    FindNextSource();
	}
    }
}

void ASMStreamInfoItr::Reset()
{
    if (m_pSourceList)
    {
	m_ulStream = 0;
	
	m_itr = m_pSourceList->Begin(); 
	FindNextSource();
    }
}

void ASMStreamInfoItr::FindNextSource()
{
    BOOL bDone = FALSE;
    while(!bDone && m_itr != m_pSourceList->End())
    {
	ASMSourceInfo2* pSrc = (ASMSourceInfo2*)(*m_itr);
	
	if (pSrc && pSrc->StreamCount() > 0)
	{
	    bDone = TRUE;
	}
	else
	{
	    ++m_itr;
	}
    }
}

ASMSourceInfo2::ASMSourceInfo2(HXSource* pSource) :
    m_pSource(pSource),
    m_ulStreamCount(0),
    m_pStreams(0),
    m_ulSubscribedBw(0),
    m_ulLastSetDelivery(0),
    m_ulMasterOffer(0),
    m_bPerfectPlay(FALSE),
    m_pMasterRuleBook(0),
    m_pSubsVars(0)
{
    if (m_pSource)
    {
	m_pSource->AddRef();
	m_bPerfectPlay = m_pSource->IsPerfectPlay();


        m_ulStreamCount = pSource->GetStreamCount();     
	m_pStreams = new ASMStreamInfo2*[m_ulStreamCount];

	if (m_pStreams)
	{
	    for (UINT32 i = 0; i < m_ulStreamCount; i++)
	    {
		IUnknown* pStream = 0;

		HX_VERIFY(HXR_OK == pSource->GetStream(i, pStream));

		m_pStreams[i] = new ASMStreamInfo2(pStream);
	    
		HX_RELEASE(pStream);
	    }
	}

	// Get Master Rulebook
	if (pSource->m_pFileHeader)
	{
	    IHXBuffer* pMasterRuleBook = NULL;
	    pSource->m_pFileHeader->
		GetPropertyCString("ASMRuleBook", pMasterRuleBook);
	    
	    if (pMasterRuleBook)
	    {
		m_pMasterRuleBook = new ASMRuleBook
		    ((const char *)pMasterRuleBook->GetBuffer());
	    }
	    
	    HX_RELEASE(pMasterRuleBook);
	}
    }
}

ASMSourceInfo2::~ASMSourceInfo2()
{
    if (m_pStreams)
    {
	for (UINT32 i = 0; i < m_ulStreamCount; i++)
	{
	    delete m_pStreams[i];
	    m_pStreams[i] = 0;
	}

	delete [] m_pStreams;
    }

    HX_RELEASE(m_pSource);
    HX_RELEASE(m_pSubsVars);

    delete m_pMasterRuleBook;
    m_pMasterRuleBook = 0;
}

inline
UINT32 ASMSourceInfo2::StreamCount() const
{
    return m_ulStreamCount;
}

ASMStreamInfo2* ASMSourceInfo2::GetStream(UINT32 i)
{
    ASMStreamInfo2* pRet = 0;

    if (m_pStreams && (i < m_ulStreamCount))
    {
	pRet = m_pStreams[i];
    }

    return pRet;
}

inline
UINT32& ASMSourceInfo2::SubscribeBw()
{
    return m_ulSubscribedBw;
}

inline
UINT32& ASMSourceInfo2::LastSetDelivery()
{
    return m_ulLastSetDelivery;
}

inline
UINT32& ASMSourceInfo2::MasterOffer()
{
    return m_ulMasterOffer;
}

inline
BOOL ASMSourceInfo2::IsMySource(HXSource* pSource)
{
    return (m_pSource == pSource);
}

BOOL ASMSourceInfo2::IsLive()
{
    BOOL bRet = FALSE;

    if (m_pSource)
    {
	bRet = m_pSource->IsLive();
    }

    return bRet;
}

inline
BOOL ASMSourceInfo2::IsPerfectPlay() const
{
    return m_bPerfectPlay;
}

inline
BOOL ASMSourceInfo2::HasMasterRuleBook() const
{
    return (m_pMasterRuleBook != NULL);
}

void ASMSourceInfo2::DistributeBw()
{
    if (m_pMasterRuleBook)
    {
	UINT32 ulRuleCount = m_pMasterRuleBook->GetNumRules();
	BOOL* pCurrentSubInfo = new BOOL[ulRuleCount];

	GetSubInfo(m_ulMasterOffer, pCurrentSubInfo);

	for (UINT16 idxRule = 0; idxRule < ulRuleCount; idxRule++)
	{
	    if (pCurrentSubInfo[idxRule])
	    {
		IHXValues* pProps = 0;
		// Set Distribution
		m_pMasterRuleBook->GetProperties(idxRule, pProps);

		for (UINT32 j = 0; j < m_ulStreamCount; j++)
		{
		    UINT32 ulStreamBw = GetStreamBw(pProps, j);
			
		    m_pStreams[j]->SetStreamOffer(ulStreamBw);
		}

		HX_RELEASE(pProps);
		break;
	    }
	}

	delete [] pCurrentSubInfo;
    }
}

inline
CHXSimpleList* ASMSourceInfo2::SubsChanges()
{
    return &m_subsChanges;
}

void ASMSourceInfo2::SubscribeToChanges()
{
    if (!m_subsChanges.IsEmpty() && m_pStreams && m_pStreams[0])
    {
	m_pStreams[0]->SubscribeToChanges(&m_subsChanges);

	while(!m_subsChanges.IsEmpty())
	{
	    RTSPSubscription* pSub = (RTSPSubscription*)m_subsChanges.RemoveHead();
	    delete pSub;
	}
    }
}

void ASMSourceInfo2::SetDeliveryBw(UINT32 ulMaxBw)
{
    if (m_pStreams)
    {
	UINT32 ulTotalBw = 0;
	
	// Calculate the total BW used by the streams
	for (UINT32 i = 0; i < m_ulStreamCount; i++)
	{
	    ulTotalBw += m_pStreams[i]->CurrentBw();
	}

	m_ulSubscribedBw = ulTotalBw;

	if (ulTotalBw)
	{
	    IHXThinnableSource* pThin = 0;
	    
	    if (HXR_OK == m_pSource->QueryInterface(IID_IHXThinnableSource, 
						    (void **)&pThin))
	    {
		/* Clamp bandwidth request */
		if (ulTotalBw > ulMaxBw)
		{
		    ulTotalBw = ulMaxBw;
		}
		
		pThin->SetDeliveryBandwidth(ulTotalBw, 0);
		
		HX_RELEASE(pThin);
	    }
	}
    }
}

inline
void ASMSourceInfo2::Done()
{
    HX_RELEASE(m_pSource);
}

HX_RESULT ASMSourceInfo2::GetSubInfo(UINT32 ulBandwidth,
				     BOOL* pCurrentSubInfo)
{
    HX_RESULT res = HXR_FAILED;

    if (m_pMasterRuleBook)
    {
	res = HXSMUpdateSubscriptionVars(m_pSubsVars, ulBandwidth, FALSE, 0);

	if (HXR_OK == res)
	{
	    res = m_pMasterRuleBook->GetSubscription(pCurrentSubInfo, 
						     m_pSubsVars);
	}
    }

    return res;
}

UINT32 ASMSourceInfo2::GetStreamBw(IHXValues* pProps, 
				   UINT32 ulStreamIndex) const
{
    UINT32 ulRet = 0;

    UINT8       pTemp[128];
    IHXBuffer* pBw = NULL;
    HX_RESULT   hxResult;
    
    // Don't assume that streamid == index in m_pStreams[j]
    sprintf((char *)pTemp, 
	    "Stream%ldBandwidth", 
	    m_pStreams[ulStreamIndex]->StreamID()); /* Flawfinder: ignore */
		    
    /*
     * if this tripps either there's a bug in here or
     * the content is messed up
     *
     */
    //If the Stream?Bandwidth property isn't found for
    //all streams don't worry about it. Not all streams
    //may have rule books.
    hxResult = pProps->GetPropertyCString((char *)pTemp, pBw);
    if(HXR_OK == hxResult && pBw)
    {
	ulRet = atoi((char*)pBw->GetBuffer());
	
	HX_RELEASE(pBw);
    }

    return ulRet;
}

ASMStreamInfo2::ASMStreamInfo2(IUnknown* pStream) :
    m_bFixedBwValid(FALSE),
    m_ulFixedBandwidth(0),
    m_ulNumThresholds(0),
    m_pThresholds(0),
    m_ulCurThresh(0),
    m_ulMaxEffectiveThreshold(0),
    m_pBias(0),
    m_pNegotiator(0),
    m_pRuleGather(0),
    m_ulStreamID(0),
    m_ulStreamOffer(0),
    m_ulOffer(0),
    m_ulResistanceToLower(0xffffffff)
{
    if (pStream)
    {
	if (HXR_OK == pStream->QueryInterface
	    (IID_IHXStreamBandwidthNegotiator,
	     (void **)&m_pNegotiator))
	{
	    m_pNegotiator->GetFixedBandwidth(m_ulFixedBandwidth);

	    if (m_ulFixedBandwidth)
	    {
		/* We need to check to see if the fixed bandwidth value
		 * matches the ASMProps Bandwidth value. If it doesn't then
		 * that means that we are dealing with a clip that doesn't
		 * advertise any bandwidth information. This is a corner
		 * case discover by streaming some unhinted .3gp files.
		 */
		IHXASMProps* pASMProps = NULL;
		UINT32 ulASMBw = 0;

		if ((HXR_OK == pStream->QueryInterface(IID_IHXASMProps,
						       (void**)&pASMProps))&&
		    (HXR_OK == pASMProps->GetBandwidth(ulASMBw)) &&
		    (m_ulFixedBandwidth == ulASMBw))
		{
		    m_bFixedBwValid = TRUE;
		}

		HX_RELEASE(pASMProps);
	    }
	    else
	    {
		m_ulNumThresholds = m_pNegotiator->GetNumThresholds();
		m_pThresholds = new float[m_ulNumThresholds];

		if (m_pThresholds)
		{
		    m_pNegotiator->GetThresholdInfo(m_pThresholds, 
						m_ulNumThresholds);

		    m_ulMaxEffectiveThreshold = m_ulNumThresholds - 1;
		}
	    }
	}
	
	pStream->QueryInterface(IID_IHXStreamBandwidthBias,
				(void **)&m_pBias);
	
	pStream->QueryInterface(IID_IHXAtomicRuleGather,
				(void **)&m_pRuleGather);

	// Get StreamID
	IHXStream* pHXStream = 0;

	pStream->QueryInterface(IID_IHXStream, (void**)&pHXStream);
	if (pHXStream)
        {
            m_ulStreamID = pHXStream->GetStreamNumber();
            pHXStream->Release();
        }
    }
}

ASMStreamInfo2::~ASMStreamInfo2()
{
    delete [] m_pThresholds;
    m_pThresholds = 0;

    HX_RELEASE(m_pBias);
    HX_RELEASE(m_pNegotiator);
    HX_RELEASE(m_pRuleGather);
}

HX_RESULT ASMStreamInfo2::GetBiasFactor(REF(INT32) lBiasFactor)
{
    HX_RESULT res = HXR_FAILED;
    
    if (m_pBias)
    {
	res = m_pBias->GetBiasFactor(lBiasFactor);
    }

    return res;
}

inline
BOOL ASMStreamInfo2::IsFixedBw() const
{
    return (m_ulFixedBandwidth != 0);
}

UINT32 ASMStreamInfo2::CurrentBw() const

⌨️ 快捷键说明

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