📄 rvorbis.cpp
字号:
/******************************************************************** * * * THIS FILE IS PART OF THE OGG VORBIS PROJECT SOURCE CODE. * * * * THE OGG VORBIS PROJECT SOURCE CODE IS (C) COPYRIGHT 1994-2001 * * by the XIPHOPHORUS Company http://www.xiph.org/ * ******************************************************************** function: implementation of the render plugin for RealSystem********************************************************************//* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: rvorbis.cpp,v 1.8.2.6 2004/11/24 18:29:53 acolwell Exp $ * * Portions Copyright (c) 1995-2004 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 (the "RPSL") available at * http://www.helixcommunity.org/content/rpsl unless you have licensed * the file under the current version of the RealNetworks Community * Source License (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. * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL") in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your version of * this file only under the terms of the GPL, and not to allow others * to use your version of this file under the terms of either the RPSL * or RCSL, indicate your decision by deleting the provisions above * and replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient may * use your version of this file under the terms of any one of the * RPSL, the RCSL or the GPL. * * This file is part of the Helix DNA Technology. RealNetworks is the * developer and/or licensor 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 ***** */#define INITGUID#include "hlxclib/stdio.h"#include "hlxclib/stdlib.h"#include "hlxclib/string.h"#include <ogg/ogg.h>#include <vorbis/codec.h>#include "hxtypes.h"#include "hxcom.h"#include "hxcore.h"#include "hxcomm.h"#include "ihxpckts.h"#include "hxplugn.h"#include "hxrendr.h"#include "hxausvc.h"#include "hxmon.h"#include "hxtick.h" // IsTimeLess(), IsTimeGreaterOrEqual()#include "pckunpck.h"#include "rvorbis.h"#include "ogg_depack.h" // OggDepacketizer#include "hxassert.h"#include "debug.h" // DPRINTF()#define D_VORBIS 0 //0x2000000#define NO_TIME_SET 0xffffffffconst UINT32 InitialPushdownDuration = 500;STDAPI RMACreateInstance(IUnknown **ppRendererObj){#ifdef _DEBUG debug_level() |= D_VORBIS;#endif /* _DEBUG */ *ppRendererObj = (IUnknown *)(IHXPlugin *)new CVorbisRenderer(); if (*ppRendererObj != NULL) { (*ppRendererObj)->AddRef(); return HXR_OK; } return HXR_OUTOFMEMORY;}const char *CVorbisRenderer::zm_pDescription = DESCRIPTION;const char *CVorbisRenderer::zm_pCopyright = COPYRIGHT;const char *CVorbisRenderer::zm_pMoreInfoURL = MORE_INFO_URL;const VorbisDepackInfo CVorbisRenderer::zm_depackInfo[] = { {"application/x-ogg", &OggDepacketizer::Create}, {"application/ogg", &OggDepacketizer::Create}};const VorbisTagMapping CVorbisRenderer::zm_tagInfo[] = { {"TITLE", "Title"}, {"ARTIST", "Author"}, {"AUTHOR", "Author"}, {"DESCRIPTION", "Description"}, {"COPYRIGHT", "Copyright"}};CVorbisRenderer::CVorbisRenderer(void) : m_RefCount(0), m_pClassFactory(NULL), m_bInSeekMode(FALSE), m_bIsGapInStreaming(TRUE), m_bEndOfPackets(FALSE), m_bRebuffering(FALSE), m_bIsFirstPacket(TRUE), m_lTimeOffset(0), m_VorbisState(VorbisInitialHeader), m_totalSamples(0), m_uAudioPushdownStartTS(0), m_pContext(NULL), m_pPlayer(NULL), m_pStream(NULL), m_pAudioPlayer(NULL), m_pStreamHeaderObj(NULL), m_pCurrentStream(NULL), m_pDepack(NULL), m_pStreamMimeTypes(NULL){ vorbis_info_init(&m_vi); vorbis_comment_init(&m_vc); int mimetypeCt = DepackInfoCount(); m_pStreamMimeTypes = new const char*[mimetypeCt + 1]; if (m_pStreamMimeTypes) { for (int i = 0; i < mimetypeCt; i++) { m_pStreamMimeTypes[i] = zm_depackInfo[i].m_pMimeType; } m_pStreamMimeTypes[mimetypeCt] = NULL; }}STDMETHODIMP CVorbisRenderer::GetPluginInfo( REF(BOOL) bLoadMultiple, REF(const char *) pDescription, REF(const char *) pCopyright, REF(const char *) pMoreInfoURL, REF(UINT32) versionNumber ){ bLoadMultiple = TRUE; pDescription = zm_pDescription; pCopyright = zm_pCopyright; pMoreInfoURL = zm_pMoreInfoURL; versionNumber = PLUGIN_VERSION_NUM; return HXR_OK;}STDMETHODIMP CVorbisRenderer::GetRendererInfo( REF(const char **) pStreamMimeTypes, REF(UINT32) initialGranularity ){ HX_RESULT res = HXR_FAILED; if (m_pStreamMimeTypes) { pStreamMimeTypes = m_pStreamMimeTypes; initialGranularity = TIME_SYNC_FREQ; res = HXR_OK; } return res;}STDMETHODIMP CVorbisRenderer::InitPlugin(IUnknown *pRMACore){ m_pContext = pRMACore; HX_ADDREF(m_pContext); pRMACore->QueryInterface(IID_IHXCommonClassFactory, (void **)&m_pClassFactory); if (m_pClassFactory == NULL) return HXR_NOTIMPL; return HXR_OK;}STDMETHODIMP CVorbisRenderer::StartStream(IHXStream *pStream, IHXPlayer *pPlayer){ DPRINTF(D_VORBIS,("CVorbisRenderer::StartStream()\n")); m_pStream = pStream; m_pPlayer = pPlayer; HX_ADDREF(m_pStream); HX_ADDREF(m_pPlayer); // get interface to audio player m_pPlayer->QueryInterface(IID_IHXAudioPlayer, (void **)&m_pAudioPlayer); return HXR_OK;}STDMETHODIMP CVorbisRenderer::OnHeader(IHXValues* pStreamHdr){ HX_RESULT res = HXR_FAILED; if (pStreamHdr) { IHXBuffer* pMimeBuf = NULL; if (HXR_OK == pStreamHdr->GetPropertyCString("MimeType", pMimeBuf)) { const char* pMimeType = (const char*)pMimeBuf->GetBuffer(); //printf ("CVorbisRenderer::OnHeader() mimetype = '%s'\n", // pMimeType); /* search for a depacketizer that handles this mimetype */ for (int i = 0; !m_pDepack && (i < DepackInfoCount()); i++) { if (!strcasecmp(pMimeType, zm_depackInfo[i].m_pMimeType)) { /* We found a match. Build the depacketizer */ m_pDepack = zm_depackInfo[i].m_fpCreateFunc(); } } } HX_RELEASE(pMimeBuf); ULONG32 ulDuration = NO_TIME_SET; pStreamHdr->GetPropertyULONG32("Duration", ulDuration); if (HXR_OK != pStreamHdr->GetPropertyULONG32("TrackStartTime", m_ulTrackStartTime)) { m_ulTrackStartTime = 0; } if (HXR_OK != pStreamHdr->GetPropertyULONG32("TrackEndTime", m_ulTrackEndTime)) { m_ulTrackEndTime = ulDuration; } } if (!m_pDepack) { res = HXR_OUTOFMEMORY; } else if ((HXR_OK == (res = m_pDepack->Init(m_pContext))) && (HXR_OK == (res = m_pDepack->OnStreamHeader(pStreamHdr)))) { /* cache stream header for use later */ HX_RELEASE(m_pStreamHeaderObj); m_pStreamHeaderObj = pStreamHdr; HX_ADDREF(m_pStreamHeaderObj); res = InitAudioStream(DEFAULT_VORBIS_SAMPLE_RATE, DEFAULT_VORBIS_CHANNELS); } return res;}STDMETHODIMP CVorbisRenderer::OnBegin(UINT32 ulTime){ DPRINTF(D_VORBIS,("OnBegin(%u)\n", ulTime)); return HXR_OK;}STDMETHODIMP CVorbisRenderer::GetDisplayType( REF(HX_DISPLAY_TYPE) displayType, REF(IHXBuffer *) pDisplayInfo ){ displayType = HX_DISPLAY_NONE; return HXR_OK;}STDMETHODIMP CVorbisRenderer::OnPacket(IHXPacket *pPacketObj, INT32 lTimeOffset){ HX_RESULT res = HXR_UNEXPECTED; DPRINTF(D_VORBIS,("Entering OnPacket() timestamp %u, offset %d\n", pPacketObj->GetTime(), lTimeOffset)); if (m_bInSeekMode) { // ignore pre-seek packets res = HXR_OK; } else if (m_pDepack) { if (m_bIsFirstPacket) { m_lTimeOffset = lTimeOffset; // Calculate current position based off the packet timestamp m_totalSamples = (((ogg_int64_t)pPacketObj->GetTime()) * m_audioFmt.ulSamplesPerSec) / 1000; if (VorbisPlay == m_VorbisState) { m_VorbisState = VorbisWriteInitialPushdown; m_uAudioPushdownStartTS = CurrentTime(); } m_bIsGapInStreaming = TRUE; m_bIsFirstPacket = FALSE; } res = m_pDepack->OnPacket(pPacketObj); if (!pPacketObj->IsLost()) { HX_RESULT err = HXR_OK; while ((HXR_OK == err) && (VorbisPlay != m_VorbisState)) { ogg_packet* pOp = NULL; err = m_pDepack->GetVorbisPacket(pOp); if (HXR_OK == err) { switch (m_VorbisState) { case VorbisWriteInitialPushdown: if ((pOp->packet[0] & 0x1) == 0) { DPRINTF(D_VORBIS,("Got initial pushdown packet\n")); DecodeAndRender(pOp); if (IsTimeGreaterOrEqual(CurrentTime(), m_uAudioPushdownStartTS + InitialPushdownDuration)) { m_VorbisState = VorbisPlay; } } else if (pOp->packet[0] == 1) { gotoInitialHeaderState(); res = handleInitialHeader(pOp); } else { // This isn't an ident header like // we expect. err = HXR_UNEXPECTED; } break; case VorbisInitialHeader: res = handleInitialHeader(pOp); break; case VorbisCommentHeader: if (vorbis_synthesis_headerin(&m_vi, &m_vc, pOp) >= 0) { DPRINTF(D_VORBIS,("Got Comment Header\n")); updateTACInfo(&m_vc); m_VorbisState = VorbisCodebookHeader; } break; case VorbisCodebookHeader: vorbis_synthesis_headerin(&m_vi, &m_vc, pOp); vorbis_synthesis_init(&m_vd, &m_vi); vorbis_block_init(&m_vd, &m_vb); m_VorbisState = VorbisWriteInitialPushdown; m_uAudioPushdownStartTS = CurrentTime(); DPRINTF(D_VORBIS,("Got Codebooks\n")); break; }; _ogg_free(pOp); } } if (IsRebuffering()) { EndRebuffer(); } } } DPRINTF(D_VORBIS,("Leaving OnPacket() %08x\n", res)); return res;}UINT32 CVorbisRenderer::CurrentTime() const{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -