📄 hxaudstr_new.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 ***** */
#include "hlxclib/stdio.h"
#include "hlxclib/string.h"
#include "hxresult.h"
#include "hxtypes.h"
#include "hxcom.h"
#include "hxengin.h"
#include "ihxpckts.h"
#include "hxbuffer.h"
#include "hxausvc.h"
#include "hxrasyn.h"
#include "hxprefs.h"
#include "hxerror.h"
#include "errdbg.h"
#include "chxpckts.h"
#include "hxaudply.h"
#include "hxaudstr.h"
#include "hxaudses.h"
#include "hxaudvol.h"
#include "mixengine.h"
#include "hxslist.h"
#include "hxmap.h"
#include "auderrs.h"
#include "hxtick.h"
#include "hxheap.h"
#ifdef _DEBUG
#undef HX_THIS_FILE
static const char HX_THIS_FILE[] = __FILE__;
#endif
#define CACHE_INCREMENT_SIZE 2
//#define _TESTING 1
#ifdef _TESTING
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined (_WINDOWS) || defined (_WIN32)
#include <io.h>
#endif
int g_log = -1;
#endif
/************************************************************************
* Method:
* IHXAudioStream::CHXAudioStream()
* Purpose:
* Constructor.
*/
CHXAudioStream::CHXAudioStream(CHXAudioPlayer* owner, IUnknown* pContext)
: m_lRefCount(0)
, m_wLastError(HXR_OK)
, m_pResampler(NULL)
, m_pValues(0)
, m_bDisableWrite(FALSE)
, m_ulGranularity(0)
, m_ulInputBytesPerGran(0)
, m_ulOutputBytesPerGran(0)
, m_pDataList(0)
, m_pInstantaneousList(0)
, m_pRAByToTsInList(0)
, m_pRAByToTsAdjustedList(0)
, m_bFirstWrite(TRUE)
, m_bHooksInitialized(FALSE)
, m_bInited(FALSE)
, m_bSetupDone(FALSE)
, m_bAudioFormatKnown(FALSE)
, m_ulMaxBlockSize(0)
, m_uVolume(HX_MAX_VOLUME)
, m_bMute(FALSE)
, m_bGotHooks(FALSE)
, m_llLastWriteTime(0)
, m_ulFudge(5)
, m_pInDataPtr(0)
, m_pOutDataPtr(0)
, m_bTobeTimed(TRUE)
, m_bIsFirstPacket(TRUE)
, m_bIsLive(FALSE)
, m_ulBaseTime(0)
, m_ulLiveDelay(0)
, m_bSetupToBeDone(FALSE)
, m_bCrossFadingToBeDone(FALSE)
, m_pCrossFadeStream(NULL)
, m_llCrossFadeStartTime(0)
, m_ulCrossFadeDuration(0)
, m_bFadeToThisStream(FALSE)
, m_bFadeAlreadyDone(FALSE)
, m_bRealAudioStream(FALSE)
, m_ulLastInputStartTime(0)
, m_ulLastInputEndTime(0)
, m_llLastStartTimePlayed(0)
, m_ulTSRollOver(0)
, m_bLastWriteTimeUpdated(FALSE)
, m_pCommonClassFactory(NULL)
, m_pAvailableBuffers(NULL)
, m_uCacheSize(CACHE_INCREMENT_SIZE)
, m_bCacheMayBeGrown(FALSE)
, m_bDeterminedInitialCacheSize(FALSE)
, m_bLastNMilliSecsToBeSaved(FALSE)
, m_ulLastNMilliSeconds(MINIMUM_INITIAL_PUSHDOWN)
, m_pLastNMilliSecsList(NULL)
, m_ulLastNHeadTime(0)
, m_ulLastNTailTime(0)
, m_eState(E_STOPPED)
, m_bCanBeRewound(FALSE)
, m_bAudioDeviceReflushHint(FALSE)
, m_bIsResumed(FALSE)
, m_bPlayerPause(FALSE)
, m_pPreferences(NULL)
, m_bMayNeedToRollbackTimestamp(FALSE)
, m_piPendingAudioData(NULL)
{
m_Owner = owner;
if (m_Owner)
{
m_Owner->AddRef();
}
if (pContext)
{
HX_VERIFY(HXR_OK == pContext->QueryInterface(IID_IHXCommonClassFactory,
(void**) &m_pCommonClassFactory));
}
#ifdef HELIX_FEATURE_VOLUME
m_pStreamVolume = NULL;
#endif
#if defined(HELIX_FEATURE_PREFERENCES)
if (pContext)
{
HX_VERIFY(HXR_OK == pContext->QueryInterface(IID_IHXPreferences, (void**) &m_pPreferences));
}
#endif /* HELIX_FEATURE_PREFERENCES */
m_DryNotificationMap = new CHXMapPtrToPtr;
m_pInDataPtr = new HXAudioData;
m_pOutDataPtr = new HXAudioData;
m_pMixEngine = new HXAudioSvcMixEngine() ;
};
/************************************************************************
* Method:
* IHXAudioStream::~CHXAudioStream()
* Purpose:
* Destructor. Clean up and set free.
*/
CHXAudioStream::~CHXAudioStream()
{
HX_DELETE(m_DryNotificationMap);
ResetStream();
HX_RELEASE(m_piPendingAudioData);
}
/////////////////////////////////////////////////////////////////////////
// Method:
// IUnknown::QueryInterface
// Purpose:
// Implement this to export the interfaces supported by your
// object.
//
STDMETHODIMP CHXAudioStream::QueryInterface(REFIID riid, void** ppvObj)
{
QInterfaceList qiList[] =
{
{ GET_IIDHANDLE(IID_IHXAudioStream), (IHXAudioStream*)this },
{ GET_IIDHANDLE(IID_IHXRealAudioSync), (IHXRealAudioSync*)this },
{ GET_IIDHANDLE(IID_IHXAudioStream2), (IHXAudioStream2*)this },
{ GET_IIDHANDLE(IID_IHXCommonClassFactory), (IHXCommonClassFactory*)this },
{ GET_IIDHANDLE(IID_IHXUpdateProperties), (IHXUpdateProperties*)this },
{ GET_IIDHANDLE(IID_IUnknown), (IUnknown*)(IHXAudioStream*)this },
#ifdef HELIX_FEATURE_VOLUME
{ GET_IIDHANDLE(IID_IHXVolumeAdviseSink), (IHXVolumeAdviseSink*)this },
#endif
};
return ::QIFind(qiList, QILISTSIZE(qiList), riid, ppvObj);
}
/////////////////////////////////////////////////////////////////////////
// Method:
// IUnknown::AddRef
// Purpose:
// Everyone usually implements this the same... feel free to use
// this implementation.
//
STDMETHODIMP_(ULONG32) CHXAudioStream::AddRef()
{
return InterlockedIncrement(&m_lRefCount);
}
/////////////////////////////////////////////////////////////////////////
// Method:
// IUnknown::Release
// Purpose:
// Everyone usually implements this the same... feel free to use
// this implementation.
//
STDMETHODIMP_(ULONG32) CHXAudioStream::Release()
{
if (InterlockedDecrement(&m_lRefCount) > 0)
{
return m_lRefCount;
}
delete this;
return 0;
}
/*
* IHXAudioStream methods
*/
/************************************************************************
* Method:
* IHXAudioStream::Init
* Purpose:
* Init the audio stream.
*/
STDMETHODIMP CHXAudioStream::Init
(
const HXAudioFormat* pAudioFormat,
IHXValues* pValues
)
{
if (m_bAudioFormatKnown)
{
return HXR_OK;
}
HX_RESULT theErr = HXR_OK;
m_pValues = pValues;
if (m_pValues)
{
m_pValues->AddRef();
UINT32 ulVal = 0;
m_pValues->GetPropertyULONG32("audioDeviceReflushHint", ulVal);
if (ulVal == 1)
{
SetAudioDeviceReflushHint(TRUE);
m_Owner->m_Owner->CheckIfLastNMilliSecsToBeStored();
}
}
memcpy( &m_AudioFmt, pAudioFormat, sizeof(HXAudioFormat) );
// Create the audio data list
m_pDataList = new CHXSimpleList;
if ( !m_pDataList )
theErr = HXR_OUTOFMEMORY;
if(!theErr) // check if list constructor really succeeded
{
if(!m_pDataList->IsPtrListValid())
theErr = HXR_OUTOFMEMORY;
}
m_pInstantaneousList = new CHXSimpleList;
if ( !m_pInstantaneousList || !m_pInstantaneousList->IsPtrListValid())
theErr = HXR_OUTOFMEMORY;
// Reset this so that we init the hooks
m_bFirstWrite = TRUE;
m_bHooksInitialized = FALSE;
#ifdef HELIX_FEATURE_VOLUME
if( !theErr )
{
m_pStreamVolume = (IHXVolume*)new CHXVolume;
if( m_pStreamVolume )
{
m_pStreamVolume->AddRef();
m_pStreamVolume->AddAdviseSink(this);
}
else
theErr = HXR_OUTOFMEMORY;
}
#endif
m_bAudioFormatKnown = TRUE;
if (m_bSetupToBeDone)
{
m_bSetupToBeDone = FALSE;
m_Owner->AudioFormatNowKnown();
}
if (!theErr && m_bSetupDone && !m_bInited)
{
theErr = ProcessInfo();
}
return theErr;
}
/************************************************************************
* Method:
* IHXAudioStream::Write
* Purpose:
* Write audio data to Audio Services.
*
* NOTE: If the renderer loses packets and there is no loss
* correction, then the renderer should write the next packet
* using a meaningful start time. Audio Services will play
* silence where packets are missing.
*/
STDMETHODIMP CHXAudioStream::Write
(
HXAudioData* pInData
)
{
HX_RESULT theErr = HXR_OK;
if (!pInData)
{
return HXR_INVALID_PARAMETER;
}
if (!m_bInited)
{
return HXR_NOT_INITIALIZED;
}
// Init pre-mix hooks. Call this once to set up hook info.
if ( !m_bHooksInitialized )
{
InitHooks();
}
#if defined(HELIX_FEATURE_AUDIO_INCOMPLETESAMPLE)
/* make sure that we are always handing complete sample frames
* down the chain by buffering up samples if we don't get complete frames.
*
* This is done by using a slush IHXBuffer that is just large enough
* to hold one sample frame, and a second IHXBuffer to hold sample fragments.
* This one does not own a buffer, but uses the incoming buffers.
*
* This way, we don't need any large memcpy()s. Unfortunately, we have to
* create a new slush buffer every time because we don't know how long
* this buffer will be stuck in the queue until it gets rendered.
*
* I guess we could try to be smart and only create a new one if the old one
* is still AddRef()ed but I figure it's not worth it.
*/
/* if there was a discontinuity in the audio stream, throw away pending bytes.
* This also assumes that we're starting at a sample frame boundary, which might
* be wrong -- but we really have no way to tell.
*/
if (pInData->uAudioStreamType != STREAMING_AUDIO)
{
m_ulPendingAudioBytes = 0 ;
HX_RELEASE(m_piPendingAudioData) ;
}
/* if pData is NULL, hand it through unchanged. See comment in Write2() */
if (!pInData->pData)
return Write2(pInData) ;
UINT32 ulInBytes = pInData->pData->GetSize() ; // number of bytes in incoming sample
UINT32 ulCutoffBytes = 0 ; // number of bytes that will be cut off incoming sample
/* first check if we have pending samples. */
if (m_ulPendingAudioBytes)
{
// complete sample frames would have been sent the last time around
HX_ASSERT(m_ulPendingAudioBytes < m_ulSampleFrameSize) ;
HX_ASSERT(m_piPendingAudioData) ;
/* append more bytes from the start of the incoming packet. */
ulCutoffBytes = m_ulSampleFrameSize - m_ulPendingAudioBytes ;
if (ulCutoffBytes > ulInBytes)
ulCutoffBytes = ulInBytes ;
memcpy(m_piPendingAudioData->GetBuffer() + m_ulPendingAudioBytes,
pInData->pData->GetBuffer(),
ulCutoffBytes ) ;
m_ulPendingAudioBytes += ulCutoffBytes ;
ulInBytes -= ulCutoffBytes ;
}
// if we have a complete sample frame in the slush buffer, send it.
if (m_ulPendingAudioBytes == m_ulSampleFrameSize)
{
HX_ASSERT(m_piPendingAudioData) ;
HXAudioData audioData ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -