From 04e0368758e14429b99838cac27bff29f76dbb65 Mon Sep 17 00:00:00 2001 From: "syd%netscape.com" Date: Sat, 4 Dec 1999 12:36:10 +0000 Subject: [PATCH] Convert nsISound to use URIs. On windows (Linux to follow) nsSound uses necko to read the URI data into a buffer, and then sends it to win32 PlaySound to play asynchronously. r=nisheeth r=hyatt. --- widget/public/nsISound.idl | 4 +-- widget/src/beos/nsSound.cpp | 5 +-- widget/src/gtk/nsSound.cpp | 4 ++- widget/src/gtk/nsSound.h | 4 +++ widget/src/mac/nsSound.cpp | 2 +- widget/src/windows/nsSound.cpp | 65 ++++++++++++++++++++++++++++------ widget/src/windows/nsSound.h | 4 ++- 7 files changed, 71 insertions(+), 17 deletions(-) diff --git a/widget/public/nsISound.idl b/widget/public/nsISound.idl index b5cd3d5687a..54fdd1b1c57 100644 --- a/widget/public/nsISound.idl +++ b/widget/public/nsISound.idl @@ -21,14 +21,14 @@ */ #include "nsISupports.idl" -#include "nsIFileSpec.idl" +#include "nsIURI.idl" [scriptable, uuid(B148EED1-236D-11d3-B35C-00A0CC3C1CDE)] interface nsISound : nsISupports { void Init(); - void Play(in nsIFileSpec filespec); + void Play(in nsIURI aURI); // void Stop(); void Beep(); diff --git a/widget/src/beos/nsSound.cpp b/widget/src/beos/nsSound.cpp index f31e8a4774c..c98adc12b4b 100644 --- a/widget/src/beos/nsSound.cpp +++ b/widget/src/beos/nsSound.cpp @@ -75,8 +75,9 @@ NS_METHOD nsSound::Beep() return NS_OK; } -NS_METHOD nsSound::Play(nsIFileSpec *filespec) +NS_METHOD nsSound::Play(nsIURI *aURI) { +/* char *filename; filespec->GetNativePath(&filename); @@ -89,6 +90,6 @@ NS_METHOD nsSound::Play(nsIFileSpec *filespec) mSound->StartPlaying(); nsCRT::free(filename); - +*/ return NS_OK; } diff --git a/widget/src/gtk/nsSound.cpp b/widget/src/gtk/nsSound.cpp index 6a9249f9648..47c17522e5c 100644 --- a/widget/src/gtk/nsSound.cpp +++ b/widget/src/gtk/nsSound.cpp @@ -106,8 +106,9 @@ NS_METHOD nsSound::Beep() return NS_OK; } -NS_METHOD nsSound::Play(nsIFileSpec *filespec) +NS_METHOD nsSound::Play(nsIURI *aURI) { +/* if (lib) { char *filename; @@ -121,5 +122,6 @@ NS_METHOD nsSound::Play(nsIFileSpec *filespec) return NS_OK; } +*/ return NS_OK; } diff --git a/widget/src/gtk/nsSound.h b/widget/src/gtk/nsSound.h index 41da01e0ab8..ff32e847b97 100644 --- a/widget/src/gtk/nsSound.h +++ b/widget/src/gtk/nsSound.h @@ -33,6 +33,10 @@ class nsSound : public nsISound { nsSound(); virtual ~nsSound(); + NS_IMETHOD Play(nsIURI *aURI); + NS_IMETHOD Init(void); + NS_IMETHOD Beep(void); + NS_DECL_ISUPPORTS NS_DECL_NSISOUND diff --git a/widget/src/mac/nsSound.cpp b/widget/src/mac/nsSound.cpp index bb2d626a8f0..d082e899c1d 100644 --- a/widget/src/mac/nsSound.cpp +++ b/widget/src/mac/nsSound.cpp @@ -69,7 +69,7 @@ NS_METHOD nsSound::Beep() return NS_OK; } -NS_METHOD nsSound::Play(nsIFileSpec *filespec) +NS_METHOD nsSound::Play(nsIURI *aURI) { NS_NOTYETIMPLEMENTED("nsSound::Play"); return NS_OK; diff --git a/widget/src/windows/nsSound.cpp b/widget/src/windows/nsSound.cpp index 7723824d9af..135c1fa9f36 100644 --- a/widget/src/windows/nsSound.cpp +++ b/widget/src/windows/nsSound.cpp @@ -23,11 +23,14 @@ #include "nscore.h" #include "nsIAllocator.h" #include "plstr.h" -#include "stdio.h" +#include #include #include "nsSound.h" +#include "nsIURI.h" +#include "nsNetUtil.h" +#include "prmem.h" NS_IMPL_ISUPPORTS(nsSound, nsCOMTypeInfo::GetIID()); @@ -39,6 +42,10 @@ nsSound::nsSound() nsSound::~nsSound() { + if (mPlayBuf) + PR_Free( mPlayBuf ); + if (mBuffer) + PR_Free( mBuffer ); } nsresult NS_NewSound(nsISound** aSound) @@ -46,11 +53,18 @@ nsresult NS_NewSound(nsISound** aSound) NS_PRECONDITION(aSound != nsnull, "null ptr"); if (! aSound) return NS_ERROR_NULL_POINTER; + + nsSound** mySound; *aSound = new nsSound(); if (! *aSound) return NS_ERROR_OUT_OF_MEMORY; - + mySound = (nsSound **) aSound; + (*mySound)->mBufferSize = 4098; + (*mySound)->mBuffer = (char *) PR_Malloc( (*mySound)->mBufferSize ); + if ( (*mySound)->mBuffer == (char *) NULL ) + return NS_ERROR_OUT_OF_MEMORY; + (*mySound)->mPlayBuf = (char *) NULL; NS_ADDREF(*aSound); return NS_OK; } @@ -69,15 +83,46 @@ NS_METHOD nsSound::Beep() return NS_OK; } -NS_METHOD nsSound::Play(nsIFileSpec *filespec) +NS_METHOD nsSound::Play(nsIURI *aURI) { - char *filename; - filespec->GetNativePath(&filename); - - ::PlaySound(filename, nsnull, SND_FILENAME | SND_NODEFAULT | SND_ASYNC); - - nsCRT::free(filename); - + nsresult rv; + nsIInputStream *inputStream; + PRUint32 totalLen = 0; + PRUint32 len; + + if ( mPlayBuf ) { + ::PlaySound(nsnull, nsnull, 0); // stop what might be playing so we can free + PR_Free( this->mPlayBuf ); + this->mPlayBuf = (char *) NULL; + } + rv = NS_OpenURI(&inputStream, aURI); + if (NS_FAILED(rv)) + return rv; + do { + rv = inputStream->Read(this->mBuffer, this->mBufferSize, &len); + if ( len ) { + totalLen += len; + if ( this->mPlayBuf == (char *) NULL ) { + this->mPlayBuf = (char *) PR_Malloc( len ); + if ( this->mPlayBuf == (char *) NULL ) { + NS_IF_RELEASE( inputStream ); + return NS_ERROR_OUT_OF_MEMORY; + } + memcpy( this->mPlayBuf, this->mBuffer, len ); + } + else { + this->mPlayBuf = (char *) PR_Realloc( this->mPlayBuf, totalLen ); + if ( this->mPlayBuf == (char *) NULL ) { + NS_IF_RELEASE( inputStream ); + return NS_ERROR_OUT_OF_MEMORY; + } + memcpy( this->mPlayBuf + (totalLen - len), this->mBuffer, len ); + } + } + } while (len > 0); + if ( this->mPlayBuf != (char *) NULL ) + ::PlaySound(this->mPlayBuf, nsnull, SND_MEMORY | SND_NODEFAULT | SND_ASYNC); + NS_IF_RELEASE( inputStream ); return NS_OK; } diff --git a/widget/src/windows/nsSound.h b/widget/src/windows/nsSound.h index 43181327ca2..70d9744b9b4 100644 --- a/widget/src/windows/nsSound.h +++ b/widget/src/windows/nsSound.h @@ -30,7 +30,9 @@ class nsSound : public nsISound { nsSound(); virtual ~nsSound(); - + char *mPlayBuf; + char *mBuffer; + PRInt32 mBufferSize; NS_DECL_ISUPPORTS NS_DECL_NSISOUND };