Minor structural changes to nsSound, r=sfraser

This commit is contained in:
syd%netscape.com 2000-01-25 03:01:35 +00:00
Родитель 02c4cf8433
Коммит 382502a6c1
2 изменённых файлов: 43 добавлений и 22 удалений

Просмотреть файл

@ -38,6 +38,7 @@ NS_IMPL_ISUPPORTS(nsSound, nsCOMTypeInfo<nsISound>::GetIID());
nsSound::nsSound() nsSound::nsSound()
{ {
NS_INIT_REFCNT(); NS_INIT_REFCNT();
mInited = PR_FALSE;
} }
nsSound::~nsSound() nsSound::~nsSound()
@ -46,33 +47,50 @@ nsSound::~nsSound()
PR_Free( mPlayBuf ); PR_Free( mPlayBuf );
if (mBuffer) if (mBuffer)
PR_Free( mBuffer ); PR_Free( mBuffer );
mInited = PR_FALSE;
} }
nsresult NS_NewSound(nsISound** aSound) nsresult NS_NewSound(nsISound** aSound)
{ {
NS_PRECONDITION(aSound != nsnull, "null ptr"); NS_PRECONDITION(aSound != nsnull, "null ptr");
if (! aSound) if (!aSound)
return NS_ERROR_NULL_POINTER; return NS_ERROR_NULL_POINTER;
nsSound** mySound; nsSound* mySound = nsnull;
NS_NEWXPCOM(mySound, nsSound);
*aSound = new nsSound(); if (!mySound)
if (! *aSound)
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
mySound = (nsSound **) aSound;
(*mySound)->mBufferSize = 4098; nsresult rv = mySound->Init();
(*mySound)->mBuffer = (char *) PR_Malloc( (*mySound)->mBufferSize ); if (NS_FAILED(rv)) {
if ( (*mySound)->mBuffer == (char *) NULL ) delete mySound;
return NS_ERROR_OUT_OF_MEMORY; return rv;
(*mySound)->mPlayBuf = (char *) NULL; }
NS_ADDREF(*aSound);
return NS_OK; // QI does the addref
return mySound->QueryInterface(NS_GET_IID(nsISound), (void**)aSound);
} }
nsresult nsSound::Init()
NS_METHOD nsSound::Init(void)
{ {
return NS_OK; return NS_OK;
}
nsresult nsSound::AllocateBuffers()
{
nsresult rv = NS_OK;
if ( mInited == PR_TRUE )
return( rv );
mBufferSize = 4098;
mBuffer = (char *) PR_Malloc( mBufferSize );
if ( mBuffer == (char *) NULL ) {
rv = NS_ERROR_OUT_OF_MEMORY;
return( rv );
}
mPlayBuf = (char *) NULL;
mInited = PR_TRUE;
return rv;
} }
@ -86,16 +104,19 @@ NS_METHOD nsSound::Beep()
NS_METHOD nsSound::Play(nsIURI *aURI) NS_METHOD nsSound::Play(nsIURI *aURI)
{ {
nsresult rv; nsresult rv;
nsIInputStream *inputStream; nsCOMPtr <nsIInputStream>inputStream;
PRUint32 totalLen = 0; PRUint32 totalLen = 0;
PRUint32 len; PRUint32 len;
if ( !mInited && NS_FAILED((rv = AllocateBuffers())) )
return rv;
if ( mPlayBuf ) { if ( mPlayBuf ) {
::PlaySound(nsnull, nsnull, 0); // stop what might be playing so we can free ::PlaySound(nsnull, nsnull, 0); // stop what might be playing so we can free
PR_Free( this->mPlayBuf ); PR_Free( this->mPlayBuf );
this->mPlayBuf = (char *) NULL; this->mPlayBuf = (char *) NULL;
} }
rv = NS_OpenURI(&inputStream, aURI); rv = NS_OpenURI(getter_AddRefs(inputStream), aURI);
if (NS_FAILED(rv)) if (NS_FAILED(rv))
return rv; return rv;
do { do {
@ -105,7 +126,6 @@ NS_METHOD nsSound::Play(nsIURI *aURI)
if ( this->mPlayBuf == (char *) NULL ) { if ( this->mPlayBuf == (char *) NULL ) {
this->mPlayBuf = (char *) PR_Malloc( len ); this->mPlayBuf = (char *) PR_Malloc( len );
if ( this->mPlayBuf == (char *) NULL ) { if ( this->mPlayBuf == (char *) NULL ) {
NS_IF_RELEASE( inputStream );
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
} }
memcpy( this->mPlayBuf, this->mBuffer, len ); memcpy( this->mPlayBuf, this->mBuffer, len );
@ -113,7 +133,6 @@ NS_METHOD nsSound::Play(nsIURI *aURI)
else { else {
this->mPlayBuf = (char *) PR_Realloc( this->mPlayBuf, totalLen ); this->mPlayBuf = (char *) PR_Realloc( this->mPlayBuf, totalLen );
if ( this->mPlayBuf == (char *) NULL ) { if ( this->mPlayBuf == (char *) NULL ) {
NS_IF_RELEASE( inputStream );
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
} }
memcpy( this->mPlayBuf + (totalLen - len), this->mBuffer, len ); memcpy( this->mPlayBuf + (totalLen - len), this->mBuffer, len );
@ -122,7 +141,6 @@ NS_METHOD nsSound::Play(nsIURI *aURI)
} while (len > 0); } while (len > 0);
if ( this->mPlayBuf != (char *) NULL ) if ( this->mPlayBuf != (char *) NULL )
::PlaySound(this->mPlayBuf, nsnull, SND_MEMORY | SND_NODEFAULT | SND_ASYNC); ::PlaySound(this->mPlayBuf, nsnull, SND_MEMORY | SND_NODEFAULT | SND_ASYNC);
NS_IF_RELEASE( inputStream );
return NS_OK; return NS_OK;
} }

Просмотреть файл

@ -30,9 +30,12 @@ class nsSound : public nsISound {
nsSound(); nsSound();
virtual ~nsSound(); virtual ~nsSound();
nsresult Init();
nsresult AllocateBuffers();
char *mPlayBuf; char *mPlayBuf;
char *mBuffer; char *mBuffer;
PRInt32 mBufferSize; PRInt32 mBufferSize;
PRBool mInited;
NS_DECL_ISUPPORTS NS_DECL_ISUPPORTS
NS_DECL_NSISOUND NS_DECL_NSISOUND
}; };