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

📄 audopwave.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 "hxcom.h"
#include "hxresult.h"
#include "hxengin.h"
#include "ihxpckts.h"   // for IHXBuffer 
#include "hxslist.h"
#include "timeval.h"
#include "hxausvc.h"
#include "auderrs.h"
#include "hxaudev.h"
#include "hxaudses.h"
#include "hxtick.h"
#include "chxpckts.h"
#include "debug.h"

#include "audOpwave.h"

#include "hlxclib/errno.h"

#include "hxtick.h"

//-1 is usually considered to be no file descriptor.
const int CAudioOutOpenwave::NO_FILE_DESCRIPTOR = -1;

const int CAudioOutOpenwave::MAX_VOLUME = 100;

static const unsigned long BLOCK_SIZE = 176400;   // ~ 1s of audio, size of the write buffer

//**************************************************************************
// Prototypes.
//
static void
iodone_callback (
	op_sound_handle const	*handle,
	S32			msg,
	op_sound_buffer const	*sndbuf,
	void			*data
);

static void
iodone_callback (
op_sound_handle const	*handle,
S32			msg,
op_sound_buffer const	*sndbuf,
void			*data
)
{
	CAudioOutOpenwave *app = (CAudioOutOpenwave *) data;
	app->iodone();
}

void
CAudioOutOpenwave::iodone()
{
	if (m_bWriteDone) // huh? double buffering out of sync, we should always be writing
	{
		//OpDPRINTF("iodone: double buffering ERROR!!!\n");
		//_PushBits();
	}
	m_bWriteDone = true;
	_PushBits();
}

    
//XXXgfw We need to clean up the return values. We need to return only PN result codes
//XXXgfw and not RA_AOE codes from interface methods.
CAudioOutOpenwave::CAudioOutOpenwave() :
    m_pCallback(NULL),
    m_wState( RA_AOS_CLOSED ),
    m_wLastError( RA_AOE_NOERR ),
    m_bMixerPresent(FALSE),
    m_wBlockSize(0),
    m_ulLastNumBytes (0),
    m_ulTotalWritten(0),
    m_bFirstWrite (TRUE),
    m_bInitCallback (TRUE),
    m_pPlaybackCountCBTime(0),
    m_PendingCallbackID (0),
    m_bCallbackPending(FALSE),
    m_pWriteList(NULL),
    m_ulDeviceBufferSize(0),
    m_ulLastTick(0),
    m_pSndDev(NULL),
    m_bWriteDone(true)
{

	//Alloc a Timeval for use with the timer callback.
    m_pPlaybackCountCBTime = new Timeval;
    m_pCallback = new HXPlaybackCountCB(this);
    m_pCallback->AddRef();

    // Initialize the Openwave write buf struct
    for (int i=0; i<10; i++)
    {
	m_SndBuf[i].fSampleBuffer = malloc(BLOCK_SIZE);
	m_SndBuf[i].fNSamples		= 0;
	m_SndBuf[i].fUserData		= NULL;
	m_SndBuf[i].fState		= OP_SNDBUF_STATE_READY;
	m_SndBuf[i].fFlags		= OP_SNDBUFF_CALLBACK_ON_IODONE;
    }
    m_nCurBuf = 0;
    m_ulSampleBufSize = BLOCK_SIZE;
    
    //Alloc our write buffer list. Want to throw from here? You will, like
    //it or not.
    m_pWriteList = new CHXSimpleList();
    
}

void CAudioOutOpenwave::_initAfterContext()
{    
}

CAudioOutOpenwave::~CAudioOutOpenwave()
{
    //We must assume that _Imp_Close has already been called. If not, we are 
    //in big trouble.
    if ( m_wState != RA_AOS_CLOSED ) 
    {
        HX_ASSERT( "Device not closed in dtor." == NULL );
    }

    //Clean up the scheduler.
    HX_RELEASE( m_pScheduler );
    HX_DELETE(m_pPlaybackCountCBTime);
    HX_RELEASE(m_pCallback);
    //OpDPRINTF("dtor: Freeing sample buffer, expect an exception\n");
    for (int i=0; i<10; i++)
    	HX_DELETE(m_SndBuf[i].fSampleBuffer);
    //OpDPRINTF("dtor: Done freeing sample buffers \n");
    if (m_pSndDev) 
    {
    	//OpDPRINTF("dtor : before freeing the channel\n");
	op_sound_unregister_callback(m_pSndDev, iodone_callback);
	op_sound_freechan(m_pSndDev);
    	//OpDPRINTF("dtor : freeing the channel\n");
    }
    HX_DELETE(m_pWriteList); // Remove all elements in the list, if any
}

UINT16 CAudioOutOpenwave::_Imp_GetVolume()
{
 
    return m_uCurVolume;
}

HX_RESULT CAudioOutOpenwave::_Imp_SetVolume( const UINT16 uVolume )
{
    HX_RESULT retCode = RA_AOE_NOERR;
    
	m_uCurVolume = uVolume;

    m_wLastError = retCode;
    return m_wLastError;
}


BOOL CAudioOutOpenwave::_Imp_SupportsVolume()
{
    return TRUE;
}


HX_RESULT CAudioOutOpenwave::_Imp_Open( const HXAudioFormat* pFormat )
{
    HX_RESULT retCode = RA_AOE_NOERR;
    m_ulLastTick = GetTickCount();
	m_ulCurrentTime = 0;
	m_wState = RA_AOS_OPENING;
	m_wLastError = retCode;

	//////////////////////////////
	// open the channel
	//////////////////////////////
	
	OpError	retval;

	if (m_pSndDev) 
	{
		//_Imp_Close();	// Close last channel
		//op_sound_unregister_callback(m_pSndDev, iodone_callback);
		op_sound_freechan(m_pSndDev);
	}

	//  Procure channels.  
	if (!(m_pSndDev = op_sound_allocchan (pFormat->uChannels)))
		return HXR_FAIL;

	m_unNumChannels = pFormat->uChannels;
	m_unSampleRate = pFormat->ulSamplesPerSec;
	m_unBitsPerSample = pFormat->uBitsPerSample;
    	m_unBytesPerSec = (m_unNumChannels * (m_unBitsPerSample/8) * m_unSampleRate);

	// Convert params into Openwave enums
	op_sound_pcm_format pcmfmt = OP_PCM_FMT_INVALID;
	switch (pFormat->uBitsPerSample)
	{
	case 8 : pcmfmt = OP_PCM_FMT_U8;	break;
	case 16: pcmfmt = OP_PCM_FMT_U16_LE;	break;
	}

	op_sound_pcm_rate pcmrate = OP_PCM_RATE_44100;
	switch (pFormat->ulSamplesPerSec)
	{
	case 8000 : pcmrate = OP_PCM_RATE_8000;		break;
	case 11025: pcmrate = OP_PCM_RATE_11025;	break;
	case 16000: pcmrate = OP_PCM_RATE_16000;	break;
	case 22050: pcmrate = OP_PCM_RATE_22050;	break;
	case 32000: pcmrate = OP_PCM_RATE_32000;	break;
	case 44100: pcmrate = OP_PCM_RATE_44100;	break;
	case 48000: pcmrate = OP_PCM_RATE_48000;	break;
	case 64000: pcmrate = OP_PCM_RATE_64000;	break;
	}

	//  Configure channels for playback.  
	retval = op_sound_set_params_args
	          (m_pSndDev,
	           OP_AUDIOTAG_VOLUME, 0xFFFF, // m_uCurVolume ?
	           OP_AUDIOTAG_PCMFORMAT, pcmfmt,
		   OP_AUDIOTAG_PCMRATE, pcmrate,
	           OP_AUDIOTAG_INTERLEAVESAMPLES, true,
	           OP_AUDIOTAG_END);
	if (retval < 0)
		return HXR_FAIL;

	//  Register our callback.  
	retval = op_sound_register_callback
	          (m_pSndDev, iodone_callback, this);
	if (retval < 0)
		return HXR_FAIL;

	// Start the device
	retval = op_sound_start (m_pSndDev);
	if (retval < 0)
		return HXR_FAIL;

    //Schedule the timer callback...
    if(m_pContext && !m_pScheduler)
    {
        m_pContext->QueryInterface(IID_IHXScheduler, (void**) &m_pScheduler );
    }

    return m_wLastError;
}

HX_RESULT CAudioOutOpenwave::_Imp_Close()
{
    HX_RESULT retCode = RA_AOE_NOERR;

	if (m_pSndDev != NULL)
	{
	    // Stop the callback
	    op_sound_unregister_callback(m_pSndDev, iodone_callback);
		// Openwave error, can't free a channel when it's busy. Just free it in the destructor
		//op_sound_freechan(m_pSndDev);
		//m_pSndDev = NULL;
	}

    //Remove callback from scheduler
    if (m_bCallbackPending)
    {
        m_pScheduler->Remove(m_PendingCallbackID);
        m_bCallbackPending = FALSE;
    }

    m_wLastError = retCode;

    return m_wLastError;
}


// no support this.
HX_RESULT CAudioOutOpenwave::_Imp_Seek(ULONG32 ulSeekTime)
{
    m_wLastError = HXR_OK;
    return m_wLastError;
}

HX_RESULT CAudioOutOpenwave::_Imp_Pause()
{
    m_wLastError = HXR_OK;
	m_wState = RA_AOS_OPEN_PAUSED;

	op_sound_unregister_callback(m_pSndDev, iodone_callback);
	op_sound_pause(m_pSndDev);

    return m_wLastError;
}

HX_RESULT CAudioOutOpenwave::_Imp_Resume()
{
    //XXXgfw We really should be closing and re-opening the device to be nice to other procs.
    m_wLastError = HXR_OK;
	m_wState = RA_AOS_OPEN_PLAYING;
	m_ulLastTick = GetTickCount();

	op_sound_register_callback(m_pSndDev, iodone_callback, this);
	if (!m_bFirstWrite)
	{
		// Start the double buffering again
		m_bWriteDone = TRUE;
		_PushBits(); 
		m_bWriteDone = TRUE;
		_PushBits(); 
	}
	op_sound_resume(m_pSndDev);

    return m_wLastError;
}

HX_RESULT CAudioOutOpenwave::_Imp_Reset()
{
    HX_RESULT retCode = RA_AOE_NOERR;
    m_ulCurrentTime = 0;
	m_ulLastTick = GetTickCount();
    m_wLastError = retCode;
    return m_wLastError;
}

HX_RESULT CAudioOutOpenwave::_Imp_Drain()
{
    HX_RESULT retCode = RA_AOE_NOERR;

    m_wLastError = retCode;
    return m_wLastError;
}


HX_RESULT CAudioOutOpenwave::_Imp_CheckFormat( const HXAudioFormat* pFormat )
{
    HX_RESULT retCode = RA_AOE_NOERR;
    m_wLastError       = HXR_OK;

⌨️ 快捷键说明

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