2008-07-29 23:50:14 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla code.
|
|
|
|
*
|
2011-02-15 13:34:38 -08:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* the Mozilla Foundation.
|
2008-07-29 23:50:14 -07:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2011-02-15 13:34:38 -08:00
|
|
|
* Chris Double <chris.double@double.co.nz>
|
2008-07-29 23:50:14 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2010-11-16 20:14:19 -08:00
|
|
|
|
|
|
|
#include "mozilla/dom/ContentChild.h"
|
|
|
|
#include "mozilla/dom/PAudioChild.h"
|
|
|
|
#include "mozilla/dom/AudioChild.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2008-07-29 23:50:14 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "prmem.h"
|
2011-05-18 14:12:25 -07:00
|
|
|
#include "prdtoa.h"
|
2008-07-29 23:50:14 -07:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsAudioStream.h"
|
2008-11-10 14:45:09 -08:00
|
|
|
#include "nsAlgorithm.h"
|
2011-04-13 15:12:23 -07:00
|
|
|
#include "VideoUtils.h"
|
2011-05-18 14:12:25 -07:00
|
|
|
#include "mozilla/Mutex.h"
|
2008-07-29 23:50:14 -07:00
|
|
|
extern "C" {
|
2008-10-15 20:16:29 -07:00
|
|
|
#include "sydneyaudio/sydney_audio.h"
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2010-11-16 20:14:19 -08:00
|
|
|
#include "nsThreadUtils.h"
|
2011-05-28 00:03:00 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2010-08-11 18:52:36 -07:00
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
#define SA_PER_STREAM_VOLUME 1
|
|
|
|
#endif
|
|
|
|
|
2011-01-09 15:47:43 -08:00
|
|
|
// Android's audio backend is not available in content processes, so audio must
|
|
|
|
// be remoted to the parent chrome process.
|
2011-04-02 19:14:00 -07:00
|
|
|
#if defined(ANDROID)
|
2011-01-09 15:47:43 -08:00
|
|
|
#define REMOTE_AUDIO 1
|
|
|
|
#endif
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
using mozilla::TimeStamp;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PRLogModuleInfo* gAudioStreamLog = nsnull;
|
|
|
|
#endif
|
|
|
|
|
2011-09-14 12:39:50 -07:00
|
|
|
static const PRUint32 FAKE_BUFFER_SIZE = 176400;
|
2008-11-06 12:53:20 -08:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
class nsNativeAudioStream : public nsAudioStream
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
~nsNativeAudioStream();
|
|
|
|
nsNativeAudioStream();
|
2010-12-19 11:05:40 -08:00
|
|
|
|
2010-11-16 20:14:19 -08:00
|
|
|
nsresult Init(PRInt32 aNumChannels, PRInt32 aRate, SampleFormat aFormat);
|
|
|
|
void Shutdown();
|
2011-09-26 20:31:18 -07:00
|
|
|
nsresult Write(const void* aBuf, PRUint32 aFrames);
|
2010-11-16 20:14:19 -08:00
|
|
|
PRUint32 Available();
|
2011-01-16 19:03:00 -08:00
|
|
|
void SetVolume(double aVolume);
|
2010-11-16 20:14:19 -08:00
|
|
|
void Drain();
|
|
|
|
void Pause();
|
|
|
|
void Resume();
|
|
|
|
PRInt64 GetPosition();
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt64 GetPositionInFrames();
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsPaused();
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt32 GetMinWriteSize();
|
2010-11-16 20:14:19 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
double mVolume;
|
|
|
|
void* mAudioHandle;
|
|
|
|
int mRate;
|
|
|
|
int mChannels;
|
|
|
|
|
|
|
|
SampleFormat mFormat;
|
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
// True if this audio stream is paused.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mPaused;
|
2010-11-16 20:14:19 -08:00
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
// True if this stream has encountered an error.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mInError;
|
2010-11-16 20:14:19 -08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
class nsRemotedAudioStream : public nsAudioStream
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
nsRemotedAudioStream();
|
|
|
|
~nsRemotedAudioStream();
|
2010-12-19 11:05:40 -08:00
|
|
|
|
2010-11-16 20:14:19 -08:00
|
|
|
nsresult Init(PRInt32 aNumChannels, PRInt32 aRate, SampleFormat aFormat);
|
|
|
|
void Shutdown();
|
2011-09-26 20:31:18 -07:00
|
|
|
nsresult Write(const void* aBuf, PRUint32 aFrames);
|
2010-11-16 20:14:19 -08:00
|
|
|
PRUint32 Available();
|
2011-01-16 19:03:00 -08:00
|
|
|
void SetVolume(double aVolume);
|
2010-11-16 20:14:19 -08:00
|
|
|
void Drain();
|
|
|
|
void Pause();
|
|
|
|
void Resume();
|
|
|
|
PRInt64 GetPosition();
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt64 GetPositionInFrames();
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsPaused();
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt32 GetMinWriteSize();
|
2010-11-16 20:14:19 -08:00
|
|
|
|
2011-05-05 03:18:06 -07:00
|
|
|
private:
|
2010-11-25 20:13:54 -08:00
|
|
|
nsRefPtr<AudioChild> mAudioChild;
|
2010-11-16 20:14:19 -08:00
|
|
|
|
|
|
|
SampleFormat mFormat;
|
|
|
|
int mRate;
|
|
|
|
int mChannels;
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt32 mBytesPerFrame;
|
2010-11-16 20:14:19 -08:00
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
// True if this audio stream is paused.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mPaused;
|
2010-11-29 21:37:32 -08:00
|
|
|
|
2010-11-16 20:14:19 -08:00
|
|
|
friend class AudioInitEvent;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AudioInitEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2011-09-26 20:31:18 -07:00
|
|
|
AudioInitEvent(nsRemotedAudioStream* owner)
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
|
|
|
mOwner = owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
ContentChild * cpc = ContentChild::GetSingleton();
|
|
|
|
NS_ASSERTION(cpc, "Content Protocol is NULL!");
|
|
|
|
mOwner->mAudioChild = static_cast<AudioChild*> (cpc->SendPAudioConstructor(mOwner->mChannels,
|
|
|
|
mOwner->mRate,
|
|
|
|
mOwner->mFormat));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-09-26 20:31:18 -07:00
|
|
|
|
|
|
|
nsRefPtr<nsRemotedAudioStream> mOwner;
|
2010-11-16 20:14:19 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
class AudioWriteEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2010-11-25 20:13:54 -08:00
|
|
|
AudioWriteEvent(AudioChild* aChild,
|
2010-11-16 20:14:19 -08:00
|
|
|
const void* aBuf,
|
2011-09-26 20:31:18 -07:00
|
|
|
PRUint32 aNumberOfFrames,
|
|
|
|
PRUint32 aBytesPerFrame)
|
|
|
|
{
|
2010-11-25 20:13:54 -08:00
|
|
|
mAudioChild = aChild;
|
2011-09-26 20:31:18 -07:00
|
|
|
mBytesPerFrame = aBytesPerFrame;
|
|
|
|
mBuffer.Assign((const char*)aBuf, aNumberOfFrames * aBytesPerFrame);
|
2010-11-16 20:14:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2010-11-25 20:13:54 -08:00
|
|
|
if (!mAudioChild->IsIPCOpen())
|
2010-11-16 20:14:19 -08:00
|
|
|
return NS_OK;
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
mAudioChild->SendWrite(mBuffer, mBuffer.Length() / mBytesPerFrame);
|
2010-11-16 20:14:19 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-25 20:13:54 -08:00
|
|
|
nsRefPtr<AudioChild> mAudioChild;
|
2010-11-16 20:14:19 -08:00
|
|
|
nsCString mBuffer;
|
2011-09-26 20:31:18 -07:00
|
|
|
PRUint32 mBytesPerFrame;
|
2010-11-16 20:14:19 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
class AudioSetVolumeEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2011-01-16 19:03:00 -08:00
|
|
|
AudioSetVolumeEvent(AudioChild* aChild, double aVolume)
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
2010-11-25 20:13:54 -08:00
|
|
|
mAudioChild = aChild;
|
2011-01-16 19:03:00 -08:00
|
|
|
mVolume = aVolume;
|
2010-11-16 20:14:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2010-11-25 20:13:54 -08:00
|
|
|
if (!mAudioChild->IsIPCOpen())
|
2010-11-16 20:14:19 -08:00
|
|
|
return NS_OK;
|
|
|
|
|
2010-11-25 20:13:54 -08:00
|
|
|
mAudioChild->SendSetVolume(mVolume);
|
2010-11-16 20:14:19 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-09-26 20:31:18 -07:00
|
|
|
|
2010-11-25 20:13:54 -08:00
|
|
|
nsRefPtr<AudioChild> mAudioChild;
|
2011-01-16 19:03:00 -08:00
|
|
|
double mVolume;
|
2010-11-16 20:14:19 -08:00
|
|
|
};
|
|
|
|
|
2011-05-15 03:53:55 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
class AudioMinWriteSizeEvent : public nsRunnable
|
2011-05-15 03:53:55 -07:00
|
|
|
{
|
|
|
|
public:
|
2011-09-26 20:31:18 -07:00
|
|
|
AudioMinWriteSizeEvent(AudioChild* aChild)
|
2011-05-15 03:53:55 -07:00
|
|
|
{
|
|
|
|
mAudioChild = aChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
if (!mAudioChild->IsIPCOpen())
|
|
|
|
return NS_OK;
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
mAudioChild->SendMinWriteSize();
|
2011-05-15 03:53:55 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<AudioChild> mAudioChild;
|
|
|
|
};
|
|
|
|
|
2010-11-16 20:14:19 -08:00
|
|
|
class AudioDrainEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2010-11-25 20:13:54 -08:00
|
|
|
AudioDrainEvent(AudioChild* aChild)
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
2010-11-25 20:13:54 -08:00
|
|
|
mAudioChild = aChild;
|
2010-11-16 20:14:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2010-11-25 20:13:54 -08:00
|
|
|
if (!mAudioChild->IsIPCOpen())
|
2010-11-16 20:14:19 -08:00
|
|
|
return NS_OK;
|
|
|
|
|
2010-11-25 20:13:54 -08:00
|
|
|
mAudioChild->SendDrain();
|
2010-11-16 20:14:19 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-09-26 20:31:18 -07:00
|
|
|
|
2010-11-25 20:13:54 -08:00
|
|
|
nsRefPtr<AudioChild> mAudioChild;
|
2010-11-16 20:14:19 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class AudioPauseEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2011-09-28 23:19:26 -07:00
|
|
|
AudioPauseEvent(AudioChild* aChild, bool pause)
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
2010-11-25 20:13:54 -08:00
|
|
|
mAudioChild = aChild;
|
2010-11-16 20:14:19 -08:00
|
|
|
mPause = pause;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2010-11-25 20:13:54 -08:00
|
|
|
if (!mAudioChild->IsIPCOpen())
|
2010-11-16 20:14:19 -08:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
if (mPause)
|
2010-11-25 20:13:54 -08:00
|
|
|
mAudioChild->SendPause();
|
2010-11-16 20:14:19 -08:00
|
|
|
else
|
2010-11-25 20:13:54 -08:00
|
|
|
mAudioChild->SendResume();
|
2010-11-16 20:14:19 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-09-26 20:31:18 -07:00
|
|
|
|
2010-11-25 20:13:54 -08:00
|
|
|
nsRefPtr<AudioChild> mAudioChild;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mPause;
|
2010-11-16 20:14:19 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-11-25 20:13:54 -08:00
|
|
|
class AudioShutdownEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AudioShutdownEvent(AudioChild* aChild)
|
|
|
|
{
|
|
|
|
mAudioChild = aChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
if (mAudioChild->IsIPCOpen())
|
2011-01-28 21:40:23 -08:00
|
|
|
mAudioChild->SendShutdown();
|
2010-11-25 20:13:54 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-09-26 20:31:18 -07:00
|
|
|
|
2010-11-25 20:13:54 -08:00
|
|
|
nsRefPtr<AudioChild> mAudioChild;
|
|
|
|
};
|
2010-11-16 20:14:19 -08:00
|
|
|
|
2011-05-18 14:12:25 -07:00
|
|
|
static mozilla::Mutex* gVolumeScaleLock = nsnull;
|
|
|
|
|
|
|
|
static double gVolumeScale = 1.0;
|
|
|
|
|
|
|
|
static int VolumeScaleChanged(const char* aPref, void *aClosure) {
|
2011-05-28 00:03:00 -07:00
|
|
|
nsAdoptingString value = Preferences::GetString("media.volume_scale");
|
2011-05-19 02:00:39 -07:00
|
|
|
mozilla::MutexAutoLock lock(*gVolumeScaleLock);
|
2011-05-18 14:12:25 -07:00
|
|
|
if (value.IsEmpty()) {
|
|
|
|
gVolumeScale = 1.0;
|
|
|
|
} else {
|
|
|
|
NS_ConvertUTF16toUTF8 utf8(value);
|
2011-06-25 07:06:02 -07:00
|
|
|
gVolumeScale = NS_MAX<double>(0, PR_strtod(utf8.get(), nsnull));
|
2011-05-18 14:12:25 -07:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double GetVolumeScale() {
|
|
|
|
mozilla::MutexAutoLock lock(*gVolumeScaleLock);
|
|
|
|
return gVolumeScale;
|
|
|
|
}
|
2010-11-16 20:14:19 -08:00
|
|
|
|
2008-10-28 09:17:59 -07:00
|
|
|
void nsAudioStream::InitLibrary()
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
gAudioStreamLog = PR_NewLogModule("nsAudioStream");
|
|
|
|
#endif
|
2011-05-18 14:12:25 -07:00
|
|
|
gVolumeScaleLock = new mozilla::Mutex("nsAudioStream::gVolumeScaleLock");
|
|
|
|
VolumeScaleChanged(nsnull, nsnull);
|
2011-05-28 16:42:57 -07:00
|
|
|
Preferences::RegisterCallback(VolumeScaleChanged, "media.volume_scale");
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsAudioStream::ShutdownLibrary()
|
|
|
|
{
|
2011-05-28 16:42:57 -07:00
|
|
|
Preferences::UnregisterCallback(VolumeScaleChanged, "media.volume_scale");
|
2011-05-18 14:12:25 -07:00
|
|
|
delete gVolumeScaleLock;
|
|
|
|
gVolumeScaleLock = nsnull;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
|
2010-11-16 20:14:19 -08:00
|
|
|
nsIThread *
|
2010-11-29 21:37:32 -08:00
|
|
|
nsAudioStream::GetThread()
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
2011-01-03 19:55:32 -08:00
|
|
|
if (!mAudioPlaybackThread) {
|
2011-07-26 20:28:03 -07:00
|
|
|
NS_NewThread(getter_AddRefs(mAudioPlaybackThread),
|
|
|
|
nsnull,
|
|
|
|
MEDIA_THREAD_STACK_SIZE);
|
2011-01-03 19:55:32 -08:00
|
|
|
}
|
2010-11-29 21:37:32 -08:00
|
|
|
return mAudioPlaybackThread;
|
2010-11-16 20:14:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAudioStream* nsAudioStream::AllocateStream()
|
|
|
|
{
|
2011-01-09 15:47:43 -08:00
|
|
|
#if defined(REMOTE_AUDIO)
|
2010-11-16 20:14:19 -08:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
2011-09-26 20:31:18 -07:00
|
|
|
return new nsRemotedAudioStream();
|
2010-11-16 20:14:19 -08:00
|
|
|
}
|
|
|
|
#endif
|
2011-09-26 20:31:18 -07:00
|
|
|
return new nsNativeAudioStream();
|
2010-11-16 20:14:19 -08:00
|
|
|
}
|
|
|
|
|
2011-01-03 19:55:32 -08:00
|
|
|
class AsyncShutdownPlaybackThread : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AsyncShutdownPlaybackThread(nsIThread* aThread) : mThread(aThread) {}
|
|
|
|
NS_IMETHODIMP Run() { return mThread->Shutdown(); }
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsAudioStream::~nsAudioStream()
|
|
|
|
{
|
|
|
|
if (mAudioPlaybackThread) {
|
|
|
|
nsCOMPtr<nsIRunnable> event = new AsyncShutdownPlaybackThread(mAudioPlaybackThread);
|
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
nsNativeAudioStream::nsNativeAudioStream() :
|
2008-07-29 23:50:14 -07:00
|
|
|
mVolume(1.0),
|
|
|
|
mAudioHandle(0),
|
|
|
|
mRate(0),
|
2008-11-06 12:53:20 -08:00
|
|
|
mChannels(0),
|
2010-04-01 20:03:07 -07:00
|
|
|
mFormat(FORMAT_S16_LE),
|
2011-09-29 16:34:37 -07:00
|
|
|
mPaused(false),
|
|
|
|
mInError(false)
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
nsNativeAudioStream::~nsNativeAudioStream()
|
2010-11-22 14:08:49 -08:00
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
2009-03-24 19:19:58 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS0(nsNativeAudioStream)
|
2010-11-16 20:14:19 -08:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
nsresult nsNativeAudioStream::Init(PRInt32 aNumChannels, PRInt32 aRate, SampleFormat aFormat)
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
|
|
|
mRate = aRate;
|
|
|
|
mChannels = aNumChannels;
|
2008-11-10 00:12:13 -08:00
|
|
|
mFormat = aFormat;
|
2010-11-16 20:14:19 -08:00
|
|
|
|
2008-07-29 23:50:14 -07:00
|
|
|
if (sa_stream_create_pcm(reinterpret_cast<sa_stream_t**>(&mAudioHandle),
|
2011-09-26 20:31:18 -07:00
|
|
|
NULL,
|
|
|
|
SA_MODE_WRONLY,
|
2009-01-29 23:27:43 -08:00
|
|
|
SA_PCM_FORMAT_S16_NE,
|
2008-07-29 23:50:14 -07:00
|
|
|
aRate,
|
|
|
|
aNumChannels) != SA_SUCCESS) {
|
|
|
|
mAudioHandle = nsnull;
|
2011-09-29 16:34:37 -07:00
|
|
|
mInError = true;
|
2011-09-26 20:31:18 -07:00
|
|
|
PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsNativeAudioStream: sa_stream_create_pcm error"));
|
2010-08-25 06:10:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
2011-09-26 20:31:18 -07:00
|
|
|
|
2008-11-10 00:12:13 -08:00
|
|
|
if (sa_stream_open(static_cast<sa_stream_t*>(mAudioHandle)) != SA_SUCCESS) {
|
|
|
|
sa_stream_destroy(static_cast<sa_stream_t*>(mAudioHandle));
|
2008-07-29 23:50:14 -07:00
|
|
|
mAudioHandle = nsnull;
|
2011-09-29 16:34:37 -07:00
|
|
|
mInError = true;
|
2011-09-26 20:31:18 -07:00
|
|
|
PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsNativeAudioStream: sa_stream_open error"));
|
2010-08-25 06:10:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
2011-09-29 16:34:37 -07:00
|
|
|
mInError = false;
|
2010-11-16 20:14:19 -08:00
|
|
|
|
2010-08-25 06:10:00 -07:00
|
|
|
return NS_OK;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
void nsNativeAudioStream::Shutdown()
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-11-16 20:14:19 -08:00
|
|
|
if (!mAudioHandle)
|
2008-10-28 09:17:59 -07:00
|
|
|
return;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-11-10 00:12:13 -08:00
|
|
|
sa_stream_destroy(static_cast<sa_stream_t*>(mAudioHandle));
|
2008-07-29 23:50:14 -07:00
|
|
|
mAudioHandle = nsnull;
|
2011-09-29 16:34:37 -07:00
|
|
|
mInError = true;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
nsresult nsNativeAudioStream::Write(const void* aBuf, PRUint32 aFrames)
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(!mPaused, "Don't write audio when paused, you'll block");
|
2008-11-10 00:12:13 -08:00
|
|
|
|
2010-08-23 19:06:14 -07:00
|
|
|
if (mInError)
|
2010-08-25 06:10:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-08-23 19:06:14 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
PRUint32 samples = aFrames * mChannels;
|
|
|
|
nsAutoArrayPtr<short> s_data(new short[samples]);
|
2008-07-29 23:50:14 -07:00
|
|
|
|
|
|
|
if (s_data) {
|
2011-05-18 14:12:25 -07:00
|
|
|
double scaled_volume = GetVolumeScale() * mVolume;
|
2008-11-10 00:12:13 -08:00
|
|
|
switch (mFormat) {
|
2008-12-15 15:06:22 -08:00
|
|
|
case FORMAT_U8: {
|
|
|
|
const PRUint8* buf = static_cast<const PRUint8*>(aBuf);
|
2011-05-18 14:12:25 -07:00
|
|
|
PRInt32 volume = PRInt32((1 << 16) * scaled_volume);
|
2011-09-26 20:31:18 -07:00
|
|
|
for (PRUint32 i = 0; i < samples; ++i) {
|
2011-08-07 17:51:04 -07:00
|
|
|
s_data[i] = short(((PRInt32(buf[i]) - 128) * volume) >> 8);
|
2008-12-15 15:06:22 -08:00
|
|
|
}
|
|
|
|
break;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
2008-12-15 15:06:22 -08:00
|
|
|
case FORMAT_S16_LE: {
|
|
|
|
const short* buf = static_cast<const short*>(aBuf);
|
2011-05-18 14:12:25 -07:00
|
|
|
PRInt32 volume = PRInt32((1 << 16) * scaled_volume);
|
2011-09-26 20:31:18 -07:00
|
|
|
for (PRUint32 i = 0; i < samples; ++i) {
|
2009-01-29 23:27:43 -08:00
|
|
|
short s = buf[i];
|
|
|
|
#if defined(IS_BIG_ENDIAN)
|
|
|
|
s = ((s & 0x00ff) << 8) | ((s & 0xff00) >> 8);
|
|
|
|
#endif
|
2011-08-07 17:51:04 -07:00
|
|
|
s_data[i] = short((PRInt32(s) * volume) >> 16);
|
2008-12-15 15:06:22 -08:00
|
|
|
}
|
|
|
|
break;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
2009-01-29 23:27:43 -08:00
|
|
|
case FORMAT_FLOAT32: {
|
2008-12-15 15:06:22 -08:00
|
|
|
const float* buf = static_cast<const float*>(aBuf);
|
2011-09-26 20:31:18 -07:00
|
|
|
for (PRUint32 i = 0; i < samples; ++i) {
|
2011-05-18 14:12:25 -07:00
|
|
|
float scaled_value = floorf(0.5 + 32768 * buf[i] * scaled_volume);
|
2008-12-15 15:06:22 -08:00
|
|
|
if (buf[i] < 0.0) {
|
2011-08-07 17:51:04 -07:00
|
|
|
s_data[i] = (scaled_value < -32768.0) ?
|
2008-12-15 15:06:22 -08:00
|
|
|
-32768 :
|
|
|
|
short(scaled_value);
|
|
|
|
} else {
|
2011-08-07 17:51:04 -07:00
|
|
|
s_data[i] = (scaled_value > 32767.0) ?
|
2008-12-15 15:06:22 -08:00
|
|
|
32767 :
|
|
|
|
short(scaled_value);
|
|
|
|
}
|
2008-11-10 00:12:13 -08:00
|
|
|
}
|
2008-12-15 15:06:22 -08:00
|
|
|
break;
|
2008-11-10 00:12:13 -08:00
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
2008-12-15 15:06:22 -08:00
|
|
|
|
|
|
|
if (sa_stream_write(static_cast<sa_stream_t*>(mAudioHandle),
|
2010-04-01 20:03:07 -07:00
|
|
|
s_data.get(),
|
2011-09-26 20:31:18 -07:00
|
|
|
samples * sizeof(short)) != SA_SUCCESS)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-09-26 20:31:18 -07:00
|
|
|
PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsNativeAudioStream: sa_stream_write error"));
|
2011-09-29 16:34:37 -07:00
|
|
|
mInError = true;
|
2010-08-25 06:10:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
2010-08-25 06:10:00 -07:00
|
|
|
return NS_OK;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
PRUint32 nsNativeAudioStream::Available()
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2008-11-06 12:53:20 -08:00
|
|
|
// If the audio backend failed to open, lie and say we'll accept some
|
|
|
|
// data.
|
2010-08-23 19:06:14 -07:00
|
|
|
if (mInError)
|
2008-11-06 12:53:20 -08:00
|
|
|
return FAKE_BUFFER_SIZE;
|
2008-10-19 00:39:21 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
size_t s = 0;
|
2009-05-14 18:29:05 -07:00
|
|
|
if (sa_stream_get_write_size(static_cast<sa_stream_t*>(mAudioHandle), &s) != SA_SUCCESS)
|
|
|
|
return 0;
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
return s / mChannels / sizeof(short);
|
2008-10-19 00:39:21 -07:00
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
void nsNativeAudioStream::SetVolume(double aVolume)
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2008-11-10 00:12:13 -08:00
|
|
|
NS_ASSERTION(aVolume >= 0.0 && aVolume <= 1.0, "Invalid volume");
|
2010-08-11 18:52:36 -07:00
|
|
|
#if defined(SA_PER_STREAM_VOLUME)
|
|
|
|
if (sa_stream_set_volume_abs(static_cast<sa_stream_t*>(mAudioHandle), aVolume) != SA_SUCCESS) {
|
2011-09-26 20:31:18 -07:00
|
|
|
PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsNativeAudioStream: sa_stream_set_volume_abs error"));
|
2011-09-29 16:34:37 -07:00
|
|
|
mInError = true;
|
2010-08-11 18:52:36 -07:00
|
|
|
}
|
|
|
|
#else
|
2008-07-29 23:50:14 -07:00
|
|
|
mVolume = aVolume;
|
2010-08-11 18:52:36 -07:00
|
|
|
#endif
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
void nsNativeAudioStream::Drain()
|
2008-11-06 12:53:20 -08:00
|
|
|
{
|
2011-02-02 15:44:40 -08:00
|
|
|
NS_ASSERTION(!mPaused, "Don't drain audio when paused, it won't finish!");
|
|
|
|
|
2010-08-23 19:06:14 -07:00
|
|
|
if (mInError)
|
2008-11-06 12:53:20 -08:00
|
|
|
return;
|
|
|
|
|
2010-04-11 15:26:34 -07:00
|
|
|
int r = sa_stream_drain(static_cast<sa_stream_t*>(mAudioHandle));
|
|
|
|
if (r != SA_SUCCESS && r != SA_ERROR_INVALID) {
|
2011-09-26 20:31:18 -07:00
|
|
|
PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsNativeAudioStream: sa_stream_drain error"));
|
2011-09-29 16:34:37 -07:00
|
|
|
mInError = true;
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
}
|
2009-05-14 18:29:05 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
void nsNativeAudioStream::Pause()
|
2009-05-14 18:29:05 -07:00
|
|
|
{
|
2010-08-23 19:06:14 -07:00
|
|
|
if (mInError)
|
2009-05-14 18:29:05 -07:00
|
|
|
return;
|
2011-09-29 16:34:37 -07:00
|
|
|
mPaused = true;
|
2009-05-14 18:29:05 -07:00
|
|
|
sa_stream_pause(static_cast<sa_stream_t*>(mAudioHandle));
|
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
void nsNativeAudioStream::Resume()
|
2009-05-14 18:29:05 -07:00
|
|
|
{
|
2010-08-23 19:06:14 -07:00
|
|
|
if (mInError)
|
2009-05-14 18:29:05 -07:00
|
|
|
return;
|
2011-09-29 16:34:37 -07:00
|
|
|
mPaused = false;
|
2009-05-14 18:29:05 -07:00
|
|
|
sa_stream_resume(static_cast<sa_stream_t*>(mAudioHandle));
|
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt64 nsNativeAudioStream::GetPosition()
|
2009-05-14 18:29:05 -07:00
|
|
|
{
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt64 position = GetPositionInFrames();
|
|
|
|
if (position >= 0) {
|
|
|
|
return ((USECS_PER_S * position) / mRate);
|
2010-08-25 06:10:00 -07:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt64 nsNativeAudioStream::GetPositionInFrames()
|
2010-08-25 06:10:00 -07:00
|
|
|
{
|
|
|
|
if (mInError) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-09-26 20:31:18 -07:00
|
|
|
|
2009-06-02 15:32:11 -07:00
|
|
|
sa_position_t positionType = SA_POSITION_WRITE_SOFTWARE;
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
positionType = SA_POSITION_WRITE_HARDWARE;
|
|
|
|
#endif
|
2011-04-21 16:16:48 -07:00
|
|
|
int64_t position = 0;
|
2009-05-14 18:29:05 -07:00
|
|
|
if (sa_stream_get_position(static_cast<sa_stream_t*>(mAudioHandle),
|
2009-06-02 15:32:11 -07:00
|
|
|
positionType, &position) == SA_SUCCESS) {
|
2011-09-26 20:31:18 -07:00
|
|
|
return position / mChannels / sizeof(short);
|
2009-05-14 18:29:05 -07:00
|
|
|
}
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
return -1;
|
2009-05-14 18:29:05 -07:00
|
|
|
}
|
2010-11-16 20:14:19 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsNativeAudioStream::IsPaused()
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
|
|
|
return mPaused;
|
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt32 nsNativeAudioStream::GetMinWriteSize()
|
2010-12-19 11:05:40 -08:00
|
|
|
{
|
2011-08-15 20:47:45 -07:00
|
|
|
size_t size;
|
2010-12-19 11:05:40 -08:00
|
|
|
int r = sa_stream_get_min_write(static_cast<sa_stream_t*>(mAudioHandle),
|
2011-08-15 20:47:45 -07:00
|
|
|
&size);
|
2011-09-26 20:31:18 -07:00
|
|
|
if (r == SA_ERROR_NOT_SUPPORTED)
|
2010-12-19 11:05:40 -08:00
|
|
|
return 1;
|
2011-09-26 20:31:18 -07:00
|
|
|
else if (r != SA_SUCCESS || size > PR_INT32_MAX)
|
2010-12-19 11:05:40 -08:00
|
|
|
return -1;
|
2011-09-26 20:31:18 -07:00
|
|
|
|
2011-08-15 20:47:45 -07:00
|
|
|
return static_cast<PRInt32>(size / mChannels / sizeof(short));
|
2010-12-19 11:05:40 -08:00
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
nsRemotedAudioStream::nsRemotedAudioStream()
|
|
|
|
: mAudioChild(nsnull),
|
2010-11-16 20:14:19 -08:00
|
|
|
mFormat(FORMAT_S16_LE),
|
|
|
|
mRate(0),
|
|
|
|
mChannels(0),
|
2011-09-26 20:31:18 -07:00
|
|
|
mBytesPerFrame(0),
|
2011-09-29 16:34:37 -07:00
|
|
|
mPaused(false)
|
2010-11-16 20:14:19 -08:00
|
|
|
{}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
nsRemotedAudioStream::~nsRemotedAudioStream()
|
2010-11-22 14:08:49 -08:00
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
2010-11-16 20:14:19 -08:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS0(nsRemotedAudioStream)
|
2010-11-16 20:14:19 -08:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
nsresult
|
|
|
|
nsRemotedAudioStream::Init(PRInt32 aNumChannels,
|
|
|
|
PRInt32 aRate,
|
|
|
|
SampleFormat aFormat)
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
|
|
|
mRate = aRate;
|
|
|
|
mChannels = aNumChannels;
|
|
|
|
mFormat = aFormat;
|
|
|
|
|
|
|
|
switch (mFormat) {
|
|
|
|
case FORMAT_U8: {
|
2011-09-26 20:31:18 -07:00
|
|
|
mBytesPerFrame = sizeof(PRUint8) * mChannels;
|
2010-11-16 20:14:19 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FORMAT_S16_LE: {
|
2011-09-26 20:31:18 -07:00
|
|
|
mBytesPerFrame = sizeof(short) * mChannels;
|
2010-11-16 20:14:19 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FORMAT_FLOAT32: {
|
2011-09-26 20:31:18 -07:00
|
|
|
mBytesPerFrame = sizeof(float) * mChannels;
|
2010-11-16 20:14:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioInitEvent(this);
|
2010-11-25 20:13:54 -08:00
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_SYNC);
|
2010-11-16 20:14:19 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-26 20:31:18 -07:00
|
|
|
nsRemotedAudioStream::Shutdown()
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
2010-11-25 20:13:54 -08:00
|
|
|
if (!mAudioChild)
|
|
|
|
return;
|
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioShutdownEvent(mAudioChild);
|
|
|
|
NS_DispatchToMainThread(event);
|
2010-11-22 14:08:49 -08:00
|
|
|
mAudioChild = nsnull;
|
2010-11-16 20:14:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-26 20:31:18 -07:00
|
|
|
nsRemotedAudioStream::Write(const void* aBuf, PRUint32 aFrames)
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
2010-11-25 20:13:54 -08:00
|
|
|
if (!mAudioChild)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioWriteEvent(mAudioChild,
|
2010-11-16 20:14:19 -08:00
|
|
|
aBuf,
|
2011-09-26 20:31:18 -07:00
|
|
|
aFrames,
|
|
|
|
mBytesPerFrame);
|
2010-11-16 20:14:19 -08:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32
|
2011-09-26 20:31:18 -07:00
|
|
|
nsRemotedAudioStream::Available()
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
|
|
|
return FAKE_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt32 nsRemotedAudioStream::GetMinWriteSize()
|
2010-12-19 11:05:40 -08:00
|
|
|
{
|
2011-05-15 03:53:55 -07:00
|
|
|
if (!mAudioChild)
|
|
|
|
return -1;
|
2011-09-26 20:31:18 -07:00
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioMinWriteSizeEvent(mAudioChild);
|
2011-05-15 03:53:55 -07:00
|
|
|
NS_DispatchToMainThread(event);
|
2011-09-26 20:31:18 -07:00
|
|
|
return mAudioChild->WaitForMinWriteSize();
|
2010-12-19 11:05:40 -08:00
|
|
|
}
|
|
|
|
|
2010-11-16 20:14:19 -08:00
|
|
|
void
|
2011-09-26 20:31:18 -07:00
|
|
|
nsRemotedAudioStream::SetVolume(double aVolume)
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
2010-11-25 20:13:54 -08:00
|
|
|
if (!mAudioChild)
|
|
|
|
return;
|
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioSetVolumeEvent(mAudioChild, aVolume);
|
2010-11-16 20:14:19 -08:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-26 20:31:18 -07:00
|
|
|
nsRemotedAudioStream::Drain()
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
2010-11-25 20:13:54 -08:00
|
|
|
if (!mAudioChild)
|
|
|
|
return;
|
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioDrainEvent(mAudioChild);
|
2010-11-29 21:37:32 -08:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
mAudioChild->WaitForDrain();
|
2010-11-16 20:14:19 -08:00
|
|
|
}
|
2011-09-26 20:31:18 -07:00
|
|
|
|
2010-11-16 20:14:19 -08:00
|
|
|
void
|
2011-09-26 20:31:18 -07:00
|
|
|
nsRemotedAudioStream::Pause()
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
2011-09-29 16:34:37 -07:00
|
|
|
mPaused = true;
|
2010-11-25 20:13:54 -08:00
|
|
|
if (!mAudioChild)
|
|
|
|
return;
|
2011-09-29 16:34:37 -07:00
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioPauseEvent(mAudioChild, true);
|
2010-11-16 20:14:19 -08:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-26 20:31:18 -07:00
|
|
|
nsRemotedAudioStream::Resume()
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
2011-09-29 16:34:37 -07:00
|
|
|
mPaused = false;
|
2010-11-25 20:13:54 -08:00
|
|
|
if (!mAudioChild)
|
|
|
|
return;
|
2011-09-29 16:34:37 -07:00
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioPauseEvent(mAudioChild, false);
|
2010-11-16 20:14:19 -08:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt64 nsRemotedAudioStream::GetPosition()
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt64 position = GetPositionInFrames();
|
|
|
|
if (position >= 0) {
|
|
|
|
return ((USECS_PER_S * position) / mRate);
|
2010-11-16 20:14:19 -08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt64
|
2011-09-26 20:31:18 -07:00
|
|
|
nsRemotedAudioStream::GetPositionInFrames()
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
|
|
|
if(!mAudioChild)
|
|
|
|
return 0;
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt64 position = mAudioChild->GetLastKnownPosition();
|
|
|
|
if (position == -1)
|
2010-11-16 20:14:19 -08:00
|
|
|
return 0;
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt64 time = mAudioChild->GetLastKnownPositionTimestamp();
|
|
|
|
PRInt64 result = position + (mRate * (PR_IntervalNow() - time) / USECS_PER_S);
|
2010-11-16 20:14:19 -08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-09-26 20:31:18 -07:00
|
|
|
nsRemotedAudioStream::IsPaused()
|
2010-11-16 20:14:19 -08:00
|
|
|
{
|
|
|
|
return mPaused;
|
|
|
|
}
|