diff --git a/mozilla/widget/src/beos/nsSound.cpp b/mozilla/widget/src/beos/nsSound.cpp index 816d46ffae5..021744e3a66 100644 --- a/mozilla/widget/src/beos/nsSound.cpp +++ b/mozilla/widget/src/beos/nsSound.cpp @@ -38,18 +38,27 @@ #include "nscore.h" -#include "nsIMemory.h" #include "plstr.h" #include - +#include "nsIURL.h" +#include "nsString.h" +#include "nsIFileURL.h" #include "nsSound.h" +#include "nsNetUtil.h" +#include "nsIPref.h" + +#include "nsDirectoryServiceDefs.h" + +#include "nsNativeCharsetUtils.h" #include #include -#include #include +#include -NS_IMPL_ISUPPORTS1(nsSound, nsISound) + + +NS_IMPL_ISUPPORTS2(nsSound, nsISound, nsIStreamLoaderObserver) //////////////////////////////////////////////////////////////////////// nsSound::nsSound() @@ -59,55 +68,104 @@ nsSound::nsSound() nsSound::~nsSound() { - delete mSound; + Init(); } -nsresult NS_NewSound(nsISound** aSound) +NS_IMETHODIMP nsSound::OnStreamComplete(nsIStreamLoader *aLoader, + nsISupports *context, + nsresult aStatus, + PRUint32 dataLen, + const PRUint8 *data) { - NS_PRECONDITION(aSound != nsnull, "null ptr"); - if (! aSound) - return NS_ERROR_NULL_POINTER; - - *aSound = new nsSound(); - if (! *aSound) - return NS_ERROR_OUT_OF_MEMORY; - - NS_ADDREF(*aSound); - return NS_OK; + // print a load error on bad status + if (NS_FAILED(aStatus)) + { +#ifdef DEBUG + printf("Failed load sound file"); +#endif + return aStatus; + } + // In theory, BeOS can play any sound format supported by MediaKit, + // we can also play it from data, but it needs parsing format and + // providing it to sound player, so let MediaKit to do it by self + static const char kSoundTmpFileName[] = "mozsound"; + nsCOMPtr soundTmp; + nsresult rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(soundTmp)); + NS_ENSURE_SUCCESS(rv, rv); + rv = soundTmp->AppendNative(nsDependentCString(kSoundTmpFileName)); + NS_ENSURE_SUCCESS(rv, rv); + nsCAutoString soundFilename; + (void) soundTmp->GetNativePath(soundFilename); + FILE *fp = fopen(soundFilename.get(), "wb+"); +#ifdef DEBUG + printf("Playing sound file%s\n",soundFilename.get()); +#endif + if (fp) + { + fwrite(data, 1, dataLen, fp); + fflush(fp); + fclose(fp); + Init(); + mSound = new BSimpleGameSound(soundFilename.get()); + if (mSound != NULL && mSound->InitCheck() == B_OK) + { + mSound->SetIsLooping(false); + mSound->StartPlaying(); + rv = NS_OK; + } + else + { + rv = NS_ERROR_FAILURE; + } + unlink(soundFilename.get()); + } + else + { + return Beep(); + } + return rv; } NS_IMETHODIMP nsSound::Init(void) { - return NS_OK; + if (mSound) + { + mSound->StopPlaying(); + delete mSound; + mSound = NULL; + } + return NS_OK; } NS_METHOD nsSound::Beep() { - ::beep(); - - return NS_OK; + ::beep(); + return NS_OK; } NS_METHOD nsSound::Play(nsIURL *aURL) { -/* - char *filename; - filespec->GetNativePath(&filename); - - delete mSound; - entry_ref ref; - BEntry e(filename); - e.GetRef(&ref); - mSound = new BSimpleGameSound(&ref); - if(mSound->InitCheck() == B_OK) - mSound->StartPlaying(); - - nsCRT::free(filename); -*/ - return NS_OK; + nsresult rv; + nsCOMPtr loader; + rv = NS_NewStreamLoader(getter_AddRefs(loader), aURL, this); + return rv; } NS_IMETHODIMP nsSound::PlaySystemSound(const nsAString &aSoundAlias) { - return Beep(); + nsresult rv = NS_ERROR_FAILURE; + if (aSoundAlias.EqualsLiteral("_moz_mailbeep")) + return Beep(); + nsCOMPtr fileURI; + // create a nsILocalFile and then a nsIFileURL from that + nsCOMPtr soundFile; + rv = NS_NewLocalFile(aSoundAlias, PR_TRUE, + getter_AddRefs(soundFile)); + NS_ENSURE_SUCCESS(rv,r v); + rv = NS_NewFileURI(getter_AddRefs(fileURI), soundFile); + NS_ENSURE_SUCCESS(rv, rv); + nsCOMPtr fileURL = do_QueryInterface(fileURI, &rv); + NS_ENSURE_SUCCESS(rv, rv); + rv = Play(fileURL); + return rv; } diff --git a/mozilla/widget/src/beos/nsSound.h b/mozilla/widget/src/beos/nsSound.h index a89a19f5660..5a70529d2f5 100644 --- a/mozilla/widget/src/beos/nsSound.h +++ b/mozilla/widget/src/beos/nsSound.h @@ -39,20 +39,22 @@ #define __nsSound_h__ #include "nsISound.h" +#include "nsIStreamLoader.h" class BSimpleGameSound; -class nsSound : public nsISound { - public: - - nsSound(); - virtual ~nsSound(); - - NS_DECL_ISUPPORTS - NS_DECL_NSISOUND - - BSimpleGameSound *mSound; +class nsSound : public nsISound, + public nsIStreamLoaderObserver +{ +public: + nsSound(); + virtual ~nsSound(); + NS_DECL_ISUPPORTS + NS_DECL_NSISOUND + NS_DECL_NSISTREAMLOADEROBSERVER +private: + BSimpleGameSound *mSound; }; #endif /* __nsSound_h__ */