Back to index

lightning-sunbird  0.9+nobinonly
nsSound.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 
00040 #include "nscore.h"
00041 #include "plstr.h"
00042 #include <stdio.h>
00043 #include "nsIURL.h"
00044 #include "nsString.h"
00045 #include "nsIFileURL.h"
00046 #include "nsSound.h"
00047 #include "nsNetUtil.h"
00048 #include "nsIPref.h"
00049 
00050 #include "nsDirectoryServiceDefs.h"
00051 
00052 #include "nsNativeCharsetUtils.h"
00053 
00054 #include <OS.h>
00055 #include <SimpleGameSound.h>
00056 #include <Beep.h>
00057 #include <unistd.h>
00058 
00059 
00060 
00061 NS_IMPL_ISUPPORTS2(nsSound, nsISound, nsIStreamLoaderObserver)
00062 
00063 
00064 nsSound::nsSound()
00065  : mSound(0)
00066 {
00067 }
00068 
00069 nsSound::~nsSound()
00070 {
00071        Init();
00072 }
00073 
00074 NS_IMETHODIMP nsSound::OnStreamComplete(nsIStreamLoader *aLoader,
00075                                         nsISupports *context,
00076                                         nsresult aStatus,
00077                                         PRUint32 dataLen,
00078                                         const PRUint8 *data)
00079 {
00080        nsresult rv = NS_ERROR_FAILURE;
00081        // print a load error on bad status
00082        if (NS_FAILED(aStatus))
00083        {
00084 #ifdef DEBUG
00085               printf("Filed load sound file");
00086 #endif
00087               return aStatus;
00088        }
00089        // In theory, BeOS can play any sound format supported by MediaKit,
00090        // also we can play it from data, but it needs parsing format and 
00091        // providing it to sound player, so let MediaKit to do it by self
00092        static const char *kSoundTmpFileName = "mozsound";
00093        nsCOMPtr<nsIFile> soundTmp;
00094        rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(soundTmp));
00095        NS_ENSURE_SUCCESS(rv,rv);
00096        rv = soundTmp->AppendNative(nsDependentCString(kSoundTmpFileName));
00097        NS_ENSURE_SUCCESS(rv,rv);
00098        nsCAutoString soundFilename;
00099        (void) soundTmp->GetNativePath(soundFilename);
00100        FILE *fp = fopen(soundFilename.get(), "wb+");
00101        //printf("Playing sound file%s\n",soundFilename.get());
00102        if (fp) 
00103        {
00104               fwrite(data, dataLen, 1, fp);
00105               fflush(fp);
00106               fclose(fp);
00107               Init();
00108               mSound = new BSimpleGameSound(soundFilename.get());
00109               if (mSound != NULL && mSound->InitCheck() == B_OK)
00110               {
00111                      mSound->SetIsLooping(false);
00112                      mSound->StartPlaying();
00113                      return NS_OK;
00114               }
00115               unlink(soundFilename.get());
00116        }
00117        else
00118        {
00119               return Beep();
00120        }
00121        return rv;
00122 }
00123 
00124 NS_IMETHODIMP nsSound::Init(void)
00125 {
00126        if (mSound)
00127        {
00128               mSound->StopPlaying();
00129               delete mSound;
00130               mSound = NULL;
00131        }
00132        return NS_OK;
00133 }
00134 
00135 NS_METHOD nsSound::Beep()
00136 {
00137        ::beep();
00138        return NS_OK;
00139 }
00140 
00141 NS_METHOD nsSound::Play(nsIURL *aURL)
00142 {
00143        nsresult rv;
00144        nsCOMPtr<nsIStreamLoader> loader;
00145        rv = NS_NewStreamLoader(getter_AddRefs(loader), aURL, this);
00146        return rv;
00147 }
00148 
00149 NS_IMETHODIMP nsSound::PlaySystemSound(const char *aSoundAlias)
00150 {
00151        nsresult rv = NS_ERROR_FAILURE;
00152        if (nsDependentCString(aSoundAlias).EqualsLiteral("_moz_mailbeep")) 
00153               return Beep();
00154        nsCOMPtr <nsIURI> fileURI;
00155        // create a nsILocalFile and then a nsIFileURL from that
00156        nsCOMPtr <nsILocalFile> soundFile;
00157        rv = NS_NewLocalFile(NS_ConvertASCIItoUTF16(aSoundAlias), PR_TRUE, 
00158                                    getter_AddRefs(soundFile));
00159        NS_ENSURE_SUCCESS(rv,rv);
00160        rv = NS_NewFileURI(getter_AddRefs(fileURI), soundFile);
00161        NS_ENSURE_SUCCESS(rv,rv);
00162        nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(fileURI,&rv);
00163        NS_ENSURE_SUCCESS(rv,rv);
00164        rv = Play(fileURL);
00165        return rv;
00166 }