2008-07-30 06:50:14 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-11-17 04:14:19 +00: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-30 06:50:14 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "prmem.h"
|
2011-05-18 21:12:25 +00:00
|
|
|
#include "prdtoa.h"
|
2008-07-30 06:50:14 +00:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsAudioStream.h"
|
2008-11-10 22:45:09 +00:00
|
|
|
#include "nsAlgorithm.h"
|
2011-04-13 22:12:23 +00:00
|
|
|
#include "VideoUtils.h"
|
2011-05-18 21:12:25 +00:00
|
|
|
#include "mozilla/Mutex.h"
|
2008-07-30 06:50:14 +00:00
|
|
|
extern "C" {
|
2008-10-16 03:16:29 +00:00
|
|
|
#include "sydneyaudio/sydney_audio.h"
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
2010-11-17 04:14:19 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2011-05-28 07:03:00 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
|
2012-01-12 21:20:36 +00:00
|
|
|
#if defined(MOZ_CUBEB)
|
|
|
|
#include "nsAutoRef.h"
|
|
|
|
#include "cubeb/cubeb.h"
|
|
|
|
#endif
|
|
|
|
|
2011-05-28 07:03:00 +00:00
|
|
|
using namespace mozilla;
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2010-08-12 01:52:36 +00:00
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
#define SA_PER_STREAM_VOLUME 1
|
|
|
|
#endif
|
|
|
|
|
2011-11-22 00:17:38 +00:00
|
|
|
// Android's audio backend is not available in content processes, so audio must
|
|
|
|
// be remoted to the parent chrome process.
|
|
|
|
#if defined(ANDROID)
|
|
|
|
#define REMOTE_AUDIO 1
|
|
|
|
#endif
|
|
|
|
|
2008-07-30 06:50:14 +00:00
|
|
|
#ifdef PR_LOGGING
|
2012-07-30 14:20:58 +00:00
|
|
|
PRLogModuleInfo* gAudioStreamLog = nullptr;
|
2008-07-30 06:50:14 +00:00
|
|
|
#endif
|
|
|
|
|
2011-09-14 19:39:50 +00:00
|
|
|
static const PRUint32 FAKE_BUFFER_SIZE = 176400;
|
2008-11-06 20:53:20 +00:00
|
|
|
|
2011-10-19 05:29:08 +00:00
|
|
|
// Number of milliseconds per second.
|
|
|
|
static const PRInt64 MS_PER_S = 1000;
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
class nsNativeAudioStream : public nsAudioStream
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
~nsNativeAudioStream();
|
|
|
|
nsNativeAudioStream();
|
2010-12-19 19:05:40 +00:00
|
|
|
|
2010-11-17 04:14:19 +00:00
|
|
|
nsresult Init(PRInt32 aNumChannels, PRInt32 aRate, SampleFormat aFormat);
|
|
|
|
void Shutdown();
|
2011-09-27 03:31:18 +00:00
|
|
|
nsresult Write(const void* aBuf, PRUint32 aFrames);
|
2010-11-17 04:14:19 +00:00
|
|
|
PRUint32 Available();
|
2011-01-17 03:03:00 +00:00
|
|
|
void SetVolume(double aVolume);
|
2010-11-17 04:14:19 +00:00
|
|
|
void Drain();
|
|
|
|
void Pause();
|
|
|
|
void Resume();
|
|
|
|
PRInt64 GetPosition();
|
2011-09-27 03:31:18 +00:00
|
|
|
PRInt64 GetPositionInFrames();
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsPaused();
|
2011-09-27 03:31:18 +00:00
|
|
|
PRInt32 GetMinWriteSize();
|
2010-11-17 04:14:19 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
double mVolume;
|
|
|
|
void* mAudioHandle;
|
|
|
|
|
2011-09-29 23:34:37 +00:00
|
|
|
// True if this audio stream is paused.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mPaused;
|
2010-11-17 04:14:19 +00:00
|
|
|
|
2011-09-29 23:34:37 +00:00
|
|
|
// True if this stream has encountered an error.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mInError;
|
2010-11-17 04:14:19 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-11-22 00:17:38 +00:00
|
|
|
#if defined(REMOTE_AUDIO)
|
2011-09-27 03:31:18 +00:00
|
|
|
class nsRemotedAudioStream : public nsAudioStream
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
nsRemotedAudioStream();
|
|
|
|
~nsRemotedAudioStream();
|
2010-12-19 19:05:40 +00:00
|
|
|
|
2010-11-17 04:14:19 +00:00
|
|
|
nsresult Init(PRInt32 aNumChannels, PRInt32 aRate, SampleFormat aFormat);
|
|
|
|
void Shutdown();
|
2011-09-27 03:31:18 +00:00
|
|
|
nsresult Write(const void* aBuf, PRUint32 aFrames);
|
2010-11-17 04:14:19 +00:00
|
|
|
PRUint32 Available();
|
2011-01-17 03:03:00 +00:00
|
|
|
void SetVolume(double aVolume);
|
2010-11-17 04:14:19 +00:00
|
|
|
void Drain();
|
|
|
|
void Pause();
|
|
|
|
void Resume();
|
|
|
|
PRInt64 GetPosition();
|
2011-09-27 03:31:18 +00:00
|
|
|
PRInt64 GetPositionInFrames();
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsPaused();
|
2011-09-27 03:31:18 +00:00
|
|
|
PRInt32 GetMinWriteSize();
|
2010-11-17 04:14:19 +00:00
|
|
|
|
2011-05-05 10:18:06 +00:00
|
|
|
private:
|
2010-11-26 04:13:54 +00:00
|
|
|
nsRefPtr<AudioChild> mAudioChild;
|
2010-11-17 04:14:19 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
PRInt32 mBytesPerFrame;
|
2010-11-17 04:14:19 +00:00
|
|
|
|
2011-09-29 23:34:37 +00:00
|
|
|
// True if this audio stream is paused.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mPaused;
|
2010-11-30 05:37:32 +00:00
|
|
|
|
2010-11-17 04:14:19 +00:00
|
|
|
friend class AudioInitEvent;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AudioInitEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2011-09-27 03:31:18 +00:00
|
|
|
AudioInitEvent(nsRemotedAudioStream* owner)
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
|
|
|
mOwner = owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
ContentChild * cpc = ContentChild::GetSingleton();
|
|
|
|
NS_ASSERTION(cpc, "Content Protocol is NULL!");
|
2012-01-12 21:20:36 +00:00
|
|
|
mOwner->mAudioChild = static_cast<AudioChild*>(cpc->SendPAudioConstructor(mOwner->mChannels,
|
|
|
|
mOwner->mRate,
|
|
|
|
mOwner->mFormat));
|
2010-11-17 04:14:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-09-27 03:31:18 +00:00
|
|
|
|
|
|
|
nsRefPtr<nsRemotedAudioStream> mOwner;
|
2010-11-17 04:14:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class AudioWriteEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2010-11-26 04:13:54 +00:00
|
|
|
AudioWriteEvent(AudioChild* aChild,
|
2010-11-17 04:14:19 +00:00
|
|
|
const void* aBuf,
|
2011-09-27 03:31:18 +00:00
|
|
|
PRUint32 aNumberOfFrames,
|
|
|
|
PRUint32 aBytesPerFrame)
|
|
|
|
{
|
2010-11-26 04:13:54 +00:00
|
|
|
mAudioChild = aChild;
|
2011-09-27 03:31:18 +00:00
|
|
|
mBytesPerFrame = aBytesPerFrame;
|
|
|
|
mBuffer.Assign((const char*)aBuf, aNumberOfFrames * aBytesPerFrame);
|
2010-11-17 04:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2010-11-26 04:13:54 +00:00
|
|
|
if (!mAudioChild->IsIPCOpen())
|
2010-11-17 04:14:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
mAudioChild->SendWrite(mBuffer, mBuffer.Length() / mBytesPerFrame);
|
2010-11-17 04:14:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-26 04:13:54 +00:00
|
|
|
nsRefPtr<AudioChild> mAudioChild;
|
2010-11-17 04:14:19 +00:00
|
|
|
nsCString mBuffer;
|
2011-09-27 03:31:18 +00:00
|
|
|
PRUint32 mBytesPerFrame;
|
2010-11-17 04:14:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class AudioSetVolumeEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2011-01-17 03:03:00 +00:00
|
|
|
AudioSetVolumeEvent(AudioChild* aChild, double aVolume)
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
2010-11-26 04:13:54 +00:00
|
|
|
mAudioChild = aChild;
|
2011-01-17 03:03:00 +00:00
|
|
|
mVolume = aVolume;
|
2010-11-17 04:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2010-11-26 04:13:54 +00:00
|
|
|
if (!mAudioChild->IsIPCOpen())
|
2010-11-17 04:14:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
2010-11-26 04:13:54 +00:00
|
|
|
mAudioChild->SendSetVolume(mVolume);
|
2010-11-17 04:14:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-09-27 03:31:18 +00:00
|
|
|
|
2010-11-26 04:13:54 +00:00
|
|
|
nsRefPtr<AudioChild> mAudioChild;
|
2011-01-17 03:03:00 +00:00
|
|
|
double mVolume;
|
2010-11-17 04:14:19 +00:00
|
|
|
};
|
|
|
|
|
2011-05-15 10:53:55 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
class AudioMinWriteSizeEvent : public nsRunnable
|
2011-05-15 10:53:55 +00:00
|
|
|
{
|
|
|
|
public:
|
2011-09-27 03:31:18 +00:00
|
|
|
AudioMinWriteSizeEvent(AudioChild* aChild)
|
2011-05-15 10:53:55 +00:00
|
|
|
{
|
|
|
|
mAudioChild = aChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
if (!mAudioChild->IsIPCOpen())
|
|
|
|
return NS_OK;
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
mAudioChild->SendMinWriteSize();
|
2011-05-15 10:53:55 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<AudioChild> mAudioChild;
|
|
|
|
};
|
|
|
|
|
2010-11-17 04:14:19 +00:00
|
|
|
class AudioDrainEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2010-11-26 04:13:54 +00:00
|
|
|
AudioDrainEvent(AudioChild* aChild)
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
2010-11-26 04:13:54 +00:00
|
|
|
mAudioChild = aChild;
|
2010-11-17 04:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2010-11-26 04:13:54 +00:00
|
|
|
if (!mAudioChild->IsIPCOpen())
|
2010-11-17 04:14:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
2010-11-26 04:13:54 +00:00
|
|
|
mAudioChild->SendDrain();
|
2010-11-17 04:14:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-09-27 03:31:18 +00:00
|
|
|
|
2010-11-26 04:13:54 +00:00
|
|
|
nsRefPtr<AudioChild> mAudioChild;
|
2010-11-17 04:14:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class AudioPauseEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2011-09-29 06:19:26 +00:00
|
|
|
AudioPauseEvent(AudioChild* aChild, bool pause)
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
2010-11-26 04:13:54 +00:00
|
|
|
mAudioChild = aChild;
|
2010-11-17 04:14:19 +00:00
|
|
|
mPause = pause;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2010-11-26 04:13:54 +00:00
|
|
|
if (!mAudioChild->IsIPCOpen())
|
2010-11-17 04:14:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
if (mPause)
|
2010-11-26 04:13:54 +00:00
|
|
|
mAudioChild->SendPause();
|
2010-11-17 04:14:19 +00:00
|
|
|
else
|
2010-11-26 04:13:54 +00:00
|
|
|
mAudioChild->SendResume();
|
2010-11-17 04:14:19 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-09-27 03:31:18 +00:00
|
|
|
|
2010-11-26 04:13:54 +00:00
|
|
|
nsRefPtr<AudioChild> mAudioChild;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mPause;
|
2010-11-17 04:14:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-11-26 04:13:54 +00:00
|
|
|
class AudioShutdownEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AudioShutdownEvent(AudioChild* aChild)
|
|
|
|
{
|
|
|
|
mAudioChild = aChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
if (mAudioChild->IsIPCOpen())
|
2011-01-29 05:40:23 +00:00
|
|
|
mAudioChild->SendShutdown();
|
2010-11-26 04:13:54 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-09-27 03:31:18 +00:00
|
|
|
|
2010-11-26 04:13:54 +00:00
|
|
|
nsRefPtr<AudioChild> mAudioChild;
|
|
|
|
};
|
2011-11-22 00:17:38 +00:00
|
|
|
#endif
|
2010-11-17 04:14:19 +00:00
|
|
|
|
2012-01-12 21:20:36 +00:00
|
|
|
#define PREF_VOLUME_SCALE "media.volume_scale"
|
|
|
|
#define PREF_USE_CUBEB "media.use_cubeb"
|
2012-05-03 04:48:54 +00:00
|
|
|
#define PREF_CUBEB_LATENCY "media.cubeb_latency_ms"
|
2011-05-18 21:12:25 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
static mozilla::Mutex* gAudioPrefsLock = nullptr;
|
2012-06-17 02:13:22 +00:00
|
|
|
static double gVolumeScale;
|
|
|
|
static bool gUseCubeb;
|
|
|
|
static PRUint32 gCubebLatency;
|
2012-05-03 04:48:54 +00:00
|
|
|
|
2012-01-12 21:20:36 +00:00
|
|
|
static int PrefChanged(const char* aPref, void* aClosure)
|
|
|
|
{
|
|
|
|
if (strcmp(aPref, PREF_VOLUME_SCALE) == 0) {
|
|
|
|
nsAdoptingString value = Preferences::GetString(aPref);
|
|
|
|
mozilla::MutexAutoLock lock(*gAudioPrefsLock);
|
|
|
|
if (value.IsEmpty()) {
|
|
|
|
gVolumeScale = 1.0;
|
|
|
|
} else {
|
|
|
|
NS_ConvertUTF16toUTF8 utf8(value);
|
2012-07-30 14:20:58 +00:00
|
|
|
gVolumeScale = NS_MAX<double>(0, PR_strtod(utf8.get(), nullptr));
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
|
|
|
} else if (strcmp(aPref, PREF_USE_CUBEB) == 0) {
|
2012-04-16 03:00:40 +00:00
|
|
|
bool value = Preferences::GetBool(aPref, true);
|
2012-01-12 21:20:36 +00:00
|
|
|
mozilla::MutexAutoLock lock(*gAudioPrefsLock);
|
|
|
|
gUseCubeb = value;
|
2012-05-03 04:48:54 +00:00
|
|
|
} else if (strcmp(aPref, PREF_CUBEB_LATENCY) == 0) {
|
2012-06-17 02:13:22 +00:00
|
|
|
// Arbitrary default stream latency of 100ms. The higher this
|
|
|
|
// value, the longer stream volume changes will take to become
|
|
|
|
// audible.
|
|
|
|
PRUint32 value = Preferences::GetUint(aPref, 100);
|
2012-05-03 04:48:54 +00:00
|
|
|
mozilla::MutexAutoLock lock(*gAudioPrefsLock);
|
|
|
|
gCubebLatency = NS_MIN<PRUint32>(NS_MAX<PRUint32>(value, 20), 1000);
|
2011-05-18 21:12:25 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 21:20:36 +00:00
|
|
|
static double GetVolumeScale()
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(*gAudioPrefsLock);
|
2011-05-18 21:12:25 +00:00
|
|
|
return gVolumeScale;
|
|
|
|
}
|
2010-11-17 04:14:19 +00:00
|
|
|
|
2012-01-12 21:20:36 +00:00
|
|
|
#if defined(MOZ_CUBEB)
|
|
|
|
static bool GetUseCubeb()
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(*gAudioPrefsLock);
|
|
|
|
return gUseCubeb;
|
|
|
|
}
|
2012-04-16 03:00:12 +00:00
|
|
|
|
|
|
|
static cubeb* gCubebContext;
|
|
|
|
|
|
|
|
static cubeb* GetCubebContext()
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(*gAudioPrefsLock);
|
|
|
|
if (gCubebContext ||
|
|
|
|
cubeb_init(&gCubebContext, "nsAudioStream") == CUBEB_OK) {
|
|
|
|
return gCubebContext;
|
|
|
|
}
|
|
|
|
NS_WARNING("cubeb_init failed");
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2012-04-16 03:00:12 +00:00
|
|
|
}
|
2012-05-03 04:48:54 +00:00
|
|
|
|
|
|
|
static PRUint32 GetCubebLatency()
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(*gAudioPrefsLock);
|
|
|
|
return gCubebLatency;
|
|
|
|
}
|
2012-01-12 21:20:36 +00:00
|
|
|
#endif
|
|
|
|
|
2008-10-28 16:17:59 +00:00
|
|
|
void nsAudioStream::InitLibrary()
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
gAudioStreamLog = PR_NewLogModule("nsAudioStream");
|
|
|
|
#endif
|
2012-01-12 21:20:36 +00:00
|
|
|
gAudioPrefsLock = new mozilla::Mutex("nsAudioStream::gAudioPrefsLock");
|
2012-07-30 14:20:58 +00:00
|
|
|
PrefChanged(PREF_VOLUME_SCALE, nullptr);
|
2012-01-12 21:20:36 +00:00
|
|
|
Preferences::RegisterCallback(PrefChanged, PREF_VOLUME_SCALE);
|
|
|
|
#if defined(MOZ_CUBEB)
|
2012-07-30 14:20:58 +00:00
|
|
|
PrefChanged(PREF_USE_CUBEB, nullptr);
|
2012-01-12 21:20:36 +00:00
|
|
|
Preferences::RegisterCallback(PrefChanged, PREF_USE_CUBEB);
|
2012-07-30 14:20:58 +00:00
|
|
|
PrefChanged(PREF_CUBEB_LATENCY, nullptr);
|
2012-06-17 02:13:22 +00:00
|
|
|
Preferences::RegisterCallback(PrefChanged, PREF_CUBEB_LATENCY);
|
2012-01-12 21:20:36 +00:00
|
|
|
#endif
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsAudioStream::ShutdownLibrary()
|
|
|
|
{
|
2012-01-12 21:20:36 +00:00
|
|
|
Preferences::UnregisterCallback(PrefChanged, PREF_VOLUME_SCALE);
|
|
|
|
#if defined(MOZ_CUBEB)
|
|
|
|
Preferences::UnregisterCallback(PrefChanged, PREF_USE_CUBEB);
|
|
|
|
#endif
|
|
|
|
delete gAudioPrefsLock;
|
2012-07-30 14:20:58 +00:00
|
|
|
gAudioPrefsLock = nullptr;
|
2012-01-12 21:20:36 +00:00
|
|
|
|
|
|
|
#if defined(MOZ_CUBEB)
|
|
|
|
if (gCubebContext) {
|
|
|
|
cubeb_destroy(gCubebContext);
|
2012-07-30 14:20:58 +00:00
|
|
|
gCubebContext = nullptr;
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
|
|
|
#endif
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
2010-11-17 04:14:19 +00:00
|
|
|
nsIThread *
|
2010-11-30 05:37:32 +00:00
|
|
|
nsAudioStream::GetThread()
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
2011-01-04 03:55:32 +00:00
|
|
|
if (!mAudioPlaybackThread) {
|
2012-06-12 17:06:20 +00:00
|
|
|
NS_NewNamedThread("Audio Stream",
|
|
|
|
getter_AddRefs(mAudioPlaybackThread),
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr,
|
2012-06-12 17:06:20 +00:00
|
|
|
MEDIA_THREAD_STACK_SIZE);
|
2011-01-04 03:55:32 +00:00
|
|
|
}
|
2010-11-30 05:37:32 +00:00
|
|
|
return mAudioPlaybackThread;
|
2010-11-17 04:14:19 +00:00
|
|
|
}
|
|
|
|
|
2011-01-04 03:55:32 +00: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-27 03:31:18 +00:00
|
|
|
nsNativeAudioStream::nsNativeAudioStream() :
|
2008-07-30 06:50:14 +00:00
|
|
|
mVolume(1.0),
|
|
|
|
mAudioHandle(0),
|
2011-09-29 23:34:37 +00:00
|
|
|
mPaused(false),
|
|
|
|
mInError(false)
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
nsNativeAudioStream::~nsNativeAudioStream()
|
2010-11-22 22:08:49 +00:00
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
2009-03-25 02:19:58 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS0(nsNativeAudioStream)
|
2010-11-17 04:14:19 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
nsresult nsNativeAudioStream::Init(PRInt32 aNumChannels, PRInt32 aRate, SampleFormat aFormat)
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
|
|
|
mRate = aRate;
|
|
|
|
mChannels = aNumChannels;
|
2008-11-10 08:12:13 +00:00
|
|
|
mFormat = aFormat;
|
2010-11-17 04:14:19 +00:00
|
|
|
|
2008-07-30 06:50:14 +00:00
|
|
|
if (sa_stream_create_pcm(reinterpret_cast<sa_stream_t**>(&mAudioHandle),
|
2011-09-27 03:31:18 +00:00
|
|
|
NULL,
|
|
|
|
SA_MODE_WRONLY,
|
2009-01-30 07:27:43 +00:00
|
|
|
SA_PCM_FORMAT_S16_NE,
|
2008-07-30 06:50:14 +00:00
|
|
|
aRate,
|
|
|
|
aNumChannels) != SA_SUCCESS) {
|
2012-07-30 14:20:58 +00:00
|
|
|
mAudioHandle = nullptr;
|
2011-09-29 23:34:37 +00:00
|
|
|
mInError = true;
|
2011-09-27 03:31:18 +00:00
|
|
|
PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsNativeAudioStream: sa_stream_create_pcm error"));
|
2010-08-25 13:10:00 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
2011-09-27 03:31:18 +00:00
|
|
|
|
2008-11-10 08:12:13 +00:00
|
|
|
if (sa_stream_open(static_cast<sa_stream_t*>(mAudioHandle)) != SA_SUCCESS) {
|
|
|
|
sa_stream_destroy(static_cast<sa_stream_t*>(mAudioHandle));
|
2012-07-30 14:20:58 +00:00
|
|
|
mAudioHandle = nullptr;
|
2011-09-29 23:34:37 +00:00
|
|
|
mInError = true;
|
2011-09-27 03:31:18 +00:00
|
|
|
PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsNativeAudioStream: sa_stream_open error"));
|
2010-08-25 13:10:00 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
2011-09-29 23:34:37 +00:00
|
|
|
mInError = false;
|
2010-11-17 04:14:19 +00:00
|
|
|
|
2010-08-25 13:10:00 +00:00
|
|
|
return NS_OK;
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
void nsNativeAudioStream::Shutdown()
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
2010-11-17 04:14:19 +00:00
|
|
|
if (!mAudioHandle)
|
2008-10-28 16:17:59 +00:00
|
|
|
return;
|
2008-07-30 06:50:14 +00:00
|
|
|
|
2008-11-10 08:12:13 +00:00
|
|
|
sa_stream_destroy(static_cast<sa_stream_t*>(mAudioHandle));
|
2012-07-30 14:20:58 +00:00
|
|
|
mAudioHandle = nullptr;
|
2011-09-29 23:34:37 +00:00
|
|
|
mInError = true;
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
nsresult nsNativeAudioStream::Write(const void* aBuf, PRUint32 aFrames)
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
2010-04-02 03:03:07 +00:00
|
|
|
NS_ASSERTION(!mPaused, "Don't write audio when paused, you'll block");
|
2008-11-10 08:12:13 +00:00
|
|
|
|
2010-08-24 02:06:14 +00:00
|
|
|
if (mInError)
|
2010-08-25 13:10:00 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-08-24 02:06:14 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
PRUint32 samples = aFrames * mChannels;
|
|
|
|
nsAutoArrayPtr<short> s_data(new short[samples]);
|
2008-07-30 06:50:14 +00:00
|
|
|
|
|
|
|
if (s_data) {
|
2011-05-18 21:12:25 +00:00
|
|
|
double scaled_volume = GetVolumeScale() * mVolume;
|
2008-11-10 08:12:13 +00:00
|
|
|
switch (mFormat) {
|
2008-12-15 23:06:22 +00:00
|
|
|
case FORMAT_U8: {
|
|
|
|
const PRUint8* buf = static_cast<const PRUint8*>(aBuf);
|
2011-05-18 21:12:25 +00:00
|
|
|
PRInt32 volume = PRInt32((1 << 16) * scaled_volume);
|
2011-09-27 03:31:18 +00:00
|
|
|
for (PRUint32 i = 0; i < samples; ++i) {
|
2011-08-08 00:51:04 +00:00
|
|
|
s_data[i] = short(((PRInt32(buf[i]) - 128) * volume) >> 8);
|
2008-12-15 23:06:22 +00:00
|
|
|
}
|
|
|
|
break;
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
2008-12-15 23:06:22 +00:00
|
|
|
case FORMAT_S16_LE: {
|
|
|
|
const short* buf = static_cast<const short*>(aBuf);
|
2011-05-18 21:12:25 +00:00
|
|
|
PRInt32 volume = PRInt32((1 << 16) * scaled_volume);
|
2011-09-27 03:31:18 +00:00
|
|
|
for (PRUint32 i = 0; i < samples; ++i) {
|
2009-01-30 07:27:43 +00:00
|
|
|
short s = buf[i];
|
|
|
|
#if defined(IS_BIG_ENDIAN)
|
|
|
|
s = ((s & 0x00ff) << 8) | ((s & 0xff00) >> 8);
|
|
|
|
#endif
|
2011-08-08 00:51:04 +00:00
|
|
|
s_data[i] = short((PRInt32(s) * volume) >> 16);
|
2008-12-15 23:06:22 +00:00
|
|
|
}
|
|
|
|
break;
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
2009-01-30 07:27:43 +00:00
|
|
|
case FORMAT_FLOAT32: {
|
2008-12-15 23:06:22 +00:00
|
|
|
const float* buf = static_cast<const float*>(aBuf);
|
2011-09-27 03:31:18 +00:00
|
|
|
for (PRUint32 i = 0; i < samples; ++i) {
|
2011-05-18 21:12:25 +00:00
|
|
|
float scaled_value = floorf(0.5 + 32768 * buf[i] * scaled_volume);
|
2008-12-15 23:06:22 +00:00
|
|
|
if (buf[i] < 0.0) {
|
2011-08-08 00:51:04 +00:00
|
|
|
s_data[i] = (scaled_value < -32768.0) ?
|
2008-12-15 23:06:22 +00:00
|
|
|
-32768 :
|
|
|
|
short(scaled_value);
|
|
|
|
} else {
|
2011-08-08 00:51:04 +00:00
|
|
|
s_data[i] = (scaled_value > 32767.0) ?
|
2008-12-15 23:06:22 +00:00
|
|
|
32767 :
|
|
|
|
short(scaled_value);
|
|
|
|
}
|
2008-11-10 08:12:13 +00:00
|
|
|
}
|
2008-12-15 23:06:22 +00:00
|
|
|
break;
|
2008-11-10 08:12:13 +00:00
|
|
|
}
|
2008-11-06 20:53:20 +00:00
|
|
|
}
|
2008-12-15 23:06:22 +00:00
|
|
|
|
|
|
|
if (sa_stream_write(static_cast<sa_stream_t*>(mAudioHandle),
|
2010-04-02 03:03:07 +00:00
|
|
|
s_data.get(),
|
2011-09-27 03:31:18 +00:00
|
|
|
samples * sizeof(short)) != SA_SUCCESS)
|
2010-04-02 03:03:07 +00:00
|
|
|
{
|
2011-09-27 03:31:18 +00:00
|
|
|
PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsNativeAudioStream: sa_stream_write error"));
|
2011-09-29 23:34:37 +00:00
|
|
|
mInError = true;
|
2010-08-25 13:10:00 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2008-11-06 20:53:20 +00:00
|
|
|
}
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
2010-08-25 13:10:00 +00:00
|
|
|
return NS_OK;
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
PRUint32 nsNativeAudioStream::Available()
|
2008-10-19 07:39:21 +00:00
|
|
|
{
|
2008-11-06 20:53:20 +00:00
|
|
|
// If the audio backend failed to open, lie and say we'll accept some
|
|
|
|
// data.
|
2010-08-24 02:06:14 +00:00
|
|
|
if (mInError)
|
2008-11-06 20:53:20 +00:00
|
|
|
return FAKE_BUFFER_SIZE;
|
2008-10-19 07:39:21 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
size_t s = 0;
|
2009-05-15 01:29:05 +00:00
|
|
|
if (sa_stream_get_write_size(static_cast<sa_stream_t*>(mAudioHandle), &s) != SA_SUCCESS)
|
|
|
|
return 0;
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
return s / mChannels / sizeof(short);
|
2008-10-19 07:39:21 +00:00
|
|
|
}
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
void nsNativeAudioStream::SetVolume(double aVolume)
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
2008-11-10 08:12:13 +00:00
|
|
|
NS_ASSERTION(aVolume >= 0.0 && aVolume <= 1.0, "Invalid volume");
|
2010-08-12 01:52:36 +00:00
|
|
|
#if defined(SA_PER_STREAM_VOLUME)
|
|
|
|
if (sa_stream_set_volume_abs(static_cast<sa_stream_t*>(mAudioHandle), aVolume) != SA_SUCCESS) {
|
2011-09-27 03:31:18 +00:00
|
|
|
PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsNativeAudioStream: sa_stream_set_volume_abs error"));
|
2011-09-29 23:34:37 +00:00
|
|
|
mInError = true;
|
2010-08-12 01:52:36 +00:00
|
|
|
}
|
|
|
|
#else
|
2008-07-30 06:50:14 +00:00
|
|
|
mVolume = aVolume;
|
2010-08-12 01:52:36 +00:00
|
|
|
#endif
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
2008-11-06 20:53:20 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
void nsNativeAudioStream::Drain()
|
2008-11-06 20:53:20 +00:00
|
|
|
{
|
2011-02-02 23:44:40 +00:00
|
|
|
NS_ASSERTION(!mPaused, "Don't drain audio when paused, it won't finish!");
|
|
|
|
|
2010-08-24 02:06:14 +00:00
|
|
|
if (mInError)
|
2008-11-06 20:53:20 +00:00
|
|
|
return;
|
|
|
|
|
2010-04-11 22:26:34 +00:00
|
|
|
int r = sa_stream_drain(static_cast<sa_stream_t*>(mAudioHandle));
|
|
|
|
if (r != SA_SUCCESS && r != SA_ERROR_INVALID) {
|
2011-09-27 03:31:18 +00:00
|
|
|
PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsNativeAudioStream: sa_stream_drain error"));
|
2011-09-29 23:34:37 +00:00
|
|
|
mInError = true;
|
2008-11-06 20:53:20 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-15 01:29:05 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
void nsNativeAudioStream::Pause()
|
2009-05-15 01:29:05 +00:00
|
|
|
{
|
2010-08-24 02:06:14 +00:00
|
|
|
if (mInError)
|
2009-05-15 01:29:05 +00:00
|
|
|
return;
|
2011-09-29 23:34:37 +00:00
|
|
|
mPaused = true;
|
2009-05-15 01:29:05 +00:00
|
|
|
sa_stream_pause(static_cast<sa_stream_t*>(mAudioHandle));
|
|
|
|
}
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
void nsNativeAudioStream::Resume()
|
2009-05-15 01:29:05 +00:00
|
|
|
{
|
2010-08-24 02:06:14 +00:00
|
|
|
if (mInError)
|
2009-05-15 01:29:05 +00:00
|
|
|
return;
|
2011-09-29 23:34:37 +00:00
|
|
|
mPaused = false;
|
2009-05-15 01:29:05 +00:00
|
|
|
sa_stream_resume(static_cast<sa_stream_t*>(mAudioHandle));
|
|
|
|
}
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
PRInt64 nsNativeAudioStream::GetPosition()
|
2009-05-15 01:29:05 +00:00
|
|
|
{
|
2011-09-27 03:31:18 +00:00
|
|
|
PRInt64 position = GetPositionInFrames();
|
|
|
|
if (position >= 0) {
|
|
|
|
return ((USECS_PER_S * position) / mRate);
|
2010-08-25 13:10:00 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
PRInt64 nsNativeAudioStream::GetPositionInFrames()
|
2010-08-25 13:10:00 +00:00
|
|
|
{
|
|
|
|
if (mInError) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-09-27 03:31:18 +00:00
|
|
|
|
2009-06-02 22:32:11 +00:00
|
|
|
sa_position_t positionType = SA_POSITION_WRITE_SOFTWARE;
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
positionType = SA_POSITION_WRITE_HARDWARE;
|
|
|
|
#endif
|
2011-04-21 23:16:48 +00:00
|
|
|
int64_t position = 0;
|
2009-05-15 01:29:05 +00:00
|
|
|
if (sa_stream_get_position(static_cast<sa_stream_t*>(mAudioHandle),
|
2009-06-02 22:32:11 +00:00
|
|
|
positionType, &position) == SA_SUCCESS) {
|
2011-09-27 03:31:18 +00:00
|
|
|
return position / mChannels / sizeof(short);
|
2009-05-15 01:29:05 +00:00
|
|
|
}
|
|
|
|
|
2010-04-02 03:03:07 +00:00
|
|
|
return -1;
|
2009-05-15 01:29:05 +00:00
|
|
|
}
|
2010-11-17 04:14:19 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool nsNativeAudioStream::IsPaused()
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
|
|
|
return mPaused;
|
|
|
|
}
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
PRInt32 nsNativeAudioStream::GetMinWriteSize()
|
2010-12-19 19:05:40 +00:00
|
|
|
{
|
2011-08-16 03:47:45 +00:00
|
|
|
size_t size;
|
2010-12-19 19:05:40 +00:00
|
|
|
int r = sa_stream_get_min_write(static_cast<sa_stream_t*>(mAudioHandle),
|
2011-08-16 03:47:45 +00:00
|
|
|
&size);
|
2011-09-27 03:31:18 +00:00
|
|
|
if (r == SA_ERROR_NOT_SUPPORTED)
|
2010-12-19 19:05:40 +00:00
|
|
|
return 1;
|
2011-09-27 03:31:18 +00:00
|
|
|
else if (r != SA_SUCCESS || size > PR_INT32_MAX)
|
2010-12-19 19:05:40 +00:00
|
|
|
return -1;
|
2011-09-27 03:31:18 +00:00
|
|
|
|
2011-08-16 03:47:45 +00:00
|
|
|
return static_cast<PRInt32>(size / mChannels / sizeof(short));
|
2010-12-19 19:05:40 +00:00
|
|
|
}
|
|
|
|
|
2011-11-22 00:17:38 +00:00
|
|
|
#if defined(REMOTE_AUDIO)
|
2011-09-27 03:31:18 +00:00
|
|
|
nsRemotedAudioStream::nsRemotedAudioStream()
|
2012-07-30 14:20:58 +00:00
|
|
|
: mAudioChild(nullptr),
|
2011-09-27 03:31:18 +00:00
|
|
|
mBytesPerFrame(0),
|
2011-09-29 23:34:37 +00:00
|
|
|
mPaused(false)
|
2010-11-17 04:14:19 +00:00
|
|
|
{}
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
nsRemotedAudioStream::~nsRemotedAudioStream()
|
2010-11-22 22:08:49 +00:00
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
2010-11-17 04:14:19 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS0(nsRemotedAudioStream)
|
2010-11-17 04:14:19 +00:00
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
nsresult
|
|
|
|
nsRemotedAudioStream::Init(PRInt32 aNumChannels,
|
|
|
|
PRInt32 aRate,
|
|
|
|
SampleFormat aFormat)
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
|
|
|
mRate = aRate;
|
|
|
|
mChannels = aNumChannels;
|
|
|
|
mFormat = aFormat;
|
|
|
|
|
|
|
|
switch (mFormat) {
|
|
|
|
case FORMAT_U8: {
|
2011-09-27 03:31:18 +00:00
|
|
|
mBytesPerFrame = sizeof(PRUint8) * mChannels;
|
2010-11-17 04:14:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FORMAT_S16_LE: {
|
2011-09-27 03:31:18 +00:00
|
|
|
mBytesPerFrame = sizeof(short) * mChannels;
|
2010-11-17 04:14:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FORMAT_FLOAT32: {
|
2011-09-27 03:31:18 +00:00
|
|
|
mBytesPerFrame = sizeof(float) * mChannels;
|
2010-11-17 04:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioInitEvent(this);
|
2010-11-26 04:13:54 +00:00
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_SYNC);
|
2010-11-17 04:14:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-27 03:31:18 +00:00
|
|
|
nsRemotedAudioStream::Shutdown()
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
2010-11-26 04:13:54 +00:00
|
|
|
if (!mAudioChild)
|
|
|
|
return;
|
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioShutdownEvent(mAudioChild);
|
|
|
|
NS_DispatchToMainThread(event);
|
2012-07-30 14:20:58 +00:00
|
|
|
mAudioChild = nullptr;
|
2010-11-17 04:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-27 03:31:18 +00:00
|
|
|
nsRemotedAudioStream::Write(const void* aBuf, PRUint32 aFrames)
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
2010-11-26 04:13:54 +00:00
|
|
|
if (!mAudioChild)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioWriteEvent(mAudioChild,
|
2010-11-17 04:14:19 +00:00
|
|
|
aBuf,
|
2011-09-27 03:31:18 +00:00
|
|
|
aFrames,
|
|
|
|
mBytesPerFrame);
|
2010-11-17 04:14:19 +00:00
|
|
|
NS_DispatchToMainThread(event);
|
2012-07-02 04:59:43 +00:00
|
|
|
mAudioChild->WaitForWrite();
|
2010-11-17 04:14:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32
|
2011-09-27 03:31:18 +00:00
|
|
|
nsRemotedAudioStream::Available()
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
|
|
|
return FAKE_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
PRInt32 nsRemotedAudioStream::GetMinWriteSize()
|
2010-12-19 19:05:40 +00:00
|
|
|
{
|
2011-05-15 10:53:55 +00:00
|
|
|
if (!mAudioChild)
|
|
|
|
return -1;
|
2011-09-27 03:31:18 +00:00
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioMinWriteSizeEvent(mAudioChild);
|
2011-05-15 10:53:55 +00:00
|
|
|
NS_DispatchToMainThread(event);
|
2011-09-27 03:31:18 +00:00
|
|
|
return mAudioChild->WaitForMinWriteSize();
|
2010-12-19 19:05:40 +00:00
|
|
|
}
|
|
|
|
|
2010-11-17 04:14:19 +00:00
|
|
|
void
|
2011-09-27 03:31:18 +00:00
|
|
|
nsRemotedAudioStream::SetVolume(double aVolume)
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
2010-11-26 04:13:54 +00:00
|
|
|
if (!mAudioChild)
|
|
|
|
return;
|
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioSetVolumeEvent(mAudioChild, aVolume);
|
2010-11-17 04:14:19 +00:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-27 03:31:18 +00:00
|
|
|
nsRemotedAudioStream::Drain()
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
2010-11-26 04:13:54 +00:00
|
|
|
if (!mAudioChild)
|
|
|
|
return;
|
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioDrainEvent(mAudioChild);
|
2010-11-30 05:37:32 +00:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
mAudioChild->WaitForDrain();
|
2010-11-17 04:14:19 +00:00
|
|
|
}
|
2011-09-27 03:31:18 +00:00
|
|
|
|
2010-11-17 04:14:19 +00:00
|
|
|
void
|
2011-09-27 03:31:18 +00:00
|
|
|
nsRemotedAudioStream::Pause()
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
2011-09-29 23:34:37 +00:00
|
|
|
mPaused = true;
|
2010-11-26 04:13:54 +00:00
|
|
|
if (!mAudioChild)
|
|
|
|
return;
|
2011-09-29 23:34:37 +00:00
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioPauseEvent(mAudioChild, true);
|
2010-11-17 04:14:19 +00:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-27 03:31:18 +00:00
|
|
|
nsRemotedAudioStream::Resume()
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
2011-09-29 23:34:37 +00:00
|
|
|
mPaused = false;
|
2010-11-26 04:13:54 +00:00
|
|
|
if (!mAudioChild)
|
|
|
|
return;
|
2011-09-29 23:34:37 +00:00
|
|
|
nsCOMPtr<nsIRunnable> event = new AudioPauseEvent(mAudioChild, false);
|
2010-11-17 04:14:19 +00:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
PRInt64 nsRemotedAudioStream::GetPosition()
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
2011-09-27 03:31:18 +00:00
|
|
|
PRInt64 position = GetPositionInFrames();
|
|
|
|
if (position >= 0) {
|
|
|
|
return ((USECS_PER_S * position) / mRate);
|
2010-11-17 04:14:19 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt64
|
2011-09-27 03:31:18 +00:00
|
|
|
nsRemotedAudioStream::GetPositionInFrames()
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
|
|
|
if(!mAudioChild)
|
|
|
|
return 0;
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
PRInt64 position = mAudioChild->GetLastKnownPosition();
|
|
|
|
if (position == -1)
|
2010-11-17 04:14:19 +00:00
|
|
|
return 0;
|
|
|
|
|
2011-09-27 03:31:18 +00:00
|
|
|
PRInt64 time = mAudioChild->GetLastKnownPositionTimestamp();
|
2011-10-19 05:29:08 +00:00
|
|
|
PRInt64 dt = PR_IntervalToMilliseconds(PR_IntervalNow() - time);
|
2010-11-17 04:14:19 +00:00
|
|
|
|
2011-10-19 05:29:08 +00:00
|
|
|
return position + (mRate * dt / MS_PER_S);
|
2010-11-17 04:14:19 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2011-09-27 03:31:18 +00:00
|
|
|
nsRemotedAudioStream::IsPaused()
|
2010-11-17 04:14:19 +00:00
|
|
|
{
|
|
|
|
return mPaused;
|
|
|
|
}
|
2011-11-22 00:17:38 +00:00
|
|
|
#endif
|
|
|
|
|
2012-01-12 21:20:36 +00:00
|
|
|
#if defined(MOZ_CUBEB)
|
|
|
|
template <>
|
|
|
|
class nsAutoRefTraits<cubeb_stream> : public nsPointerRefTraits<cubeb_stream>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void Release(cubeb_stream* aStream) { cubeb_stream_destroy(aStream); }
|
|
|
|
};
|
|
|
|
|
2012-05-03 04:48:54 +00:00
|
|
|
class nsCircularByteBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsCircularByteBuffer()
|
2012-07-30 14:20:58 +00:00
|
|
|
: mBuffer(nullptr), mCapacity(0), mStart(0), mCount(0)
|
2012-05-03 04:48:54 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
// Set the capacity of the buffer in bytes. Must be called before any
|
|
|
|
// call to append or pop elements.
|
|
|
|
void SetCapacity(PRUint32 aCapacity) {
|
|
|
|
NS_ABORT_IF_FALSE(!mBuffer, "Buffer allocated.");
|
|
|
|
mCapacity = aCapacity;
|
|
|
|
mBuffer = new PRUint8[mCapacity];
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 Length() {
|
|
|
|
return mCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 Capacity() {
|
|
|
|
return mCapacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 Available() {
|
|
|
|
return Capacity() - Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append aLength bytes from aSrc to the buffer. Caller must check that
|
|
|
|
// sufficient space is available.
|
|
|
|
void AppendElements(const PRUint8* aSrc, PRUint32 aLength) {
|
|
|
|
NS_ABORT_IF_FALSE(mBuffer && mCapacity, "Buffer not initialized.");
|
|
|
|
NS_ABORT_IF_FALSE(aLength <= Available(), "Buffer full.");
|
|
|
|
|
|
|
|
PRUint32 end = (mStart + mCount) % mCapacity;
|
|
|
|
|
|
|
|
PRUint32 toCopy = NS_MIN(mCapacity - end, aLength);
|
|
|
|
memcpy(&mBuffer[end], aSrc, toCopy);
|
|
|
|
memcpy(&mBuffer[0], aSrc + toCopy, aLength - toCopy);
|
|
|
|
mCount += aLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove aSize bytes from the buffer. Caller must check returned size in
|
|
|
|
// aSize{1,2} before using the pointer returned in aData{1,2}. Caller
|
|
|
|
// must not specify an aSize larger than Length().
|
|
|
|
void PopElements(PRUint32 aSize, void** aData1, PRUint32* aSize1,
|
|
|
|
void** aData2, PRUint32* aSize2) {
|
|
|
|
NS_ABORT_IF_FALSE(mBuffer && mCapacity, "Buffer not initialized.");
|
|
|
|
NS_ABORT_IF_FALSE(aSize <= Length(), "Request too large.");
|
|
|
|
|
|
|
|
*aData1 = &mBuffer[mStart];
|
|
|
|
*aSize1 = NS_MIN(mCapacity - mStart, aSize);
|
|
|
|
*aData2 = &mBuffer[0];
|
|
|
|
*aSize2 = aSize - *aSize1;
|
|
|
|
mCount -= *aSize1 + *aSize2;
|
|
|
|
mStart += *aSize1 + *aSize2;
|
|
|
|
mStart %= mCapacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsAutoArrayPtr<PRUint8> mBuffer;
|
|
|
|
PRUint32 mCapacity;
|
|
|
|
PRUint32 mStart;
|
|
|
|
PRUint32 mCount;
|
|
|
|
};
|
|
|
|
|
2012-01-12 21:20:36 +00:00
|
|
|
class nsBufferedAudioStream : public nsAudioStream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsBufferedAudioStream();
|
|
|
|
~nsBufferedAudioStream();
|
|
|
|
|
|
|
|
nsresult Init(PRInt32 aNumChannels, PRInt32 aRate, SampleFormat aFormat);
|
|
|
|
void Shutdown();
|
|
|
|
nsresult Write(const void* aBuf, PRUint32 aFrames);
|
|
|
|
PRUint32 Available();
|
|
|
|
void SetVolume(double aVolume);
|
|
|
|
void Drain();
|
|
|
|
void Pause();
|
|
|
|
void Resume();
|
|
|
|
PRInt64 GetPosition();
|
|
|
|
PRInt64 GetPositionInFrames();
|
|
|
|
bool IsPaused();
|
|
|
|
PRInt32 GetMinWriteSize();
|
|
|
|
|
|
|
|
private:
|
|
|
|
static long DataCallback_S(cubeb_stream*, void* aThis, void* aBuffer, long aFrames)
|
|
|
|
{
|
|
|
|
return static_cast<nsBufferedAudioStream*>(aThis)->DataCallback(aBuffer, aFrames);
|
|
|
|
}
|
|
|
|
|
2012-07-16 21:15:24 +00:00
|
|
|
static void StateCallback_S(cubeb_stream*, void* aThis, cubeb_state aState)
|
2012-01-12 21:20:36 +00:00
|
|
|
{
|
2012-07-16 21:15:24 +00:00
|
|
|
static_cast<nsBufferedAudioStream*>(aThis)->StateCallback(aState);
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long DataCallback(void* aBuffer, long aFrames);
|
2012-07-16 21:15:24 +00:00
|
|
|
void StateCallback(cubeb_state aState);
|
2012-01-12 21:20:36 +00:00
|
|
|
|
|
|
|
// Shared implementation of underflow adjusted position calculation.
|
|
|
|
// Caller must own the monitor.
|
|
|
|
PRInt64 GetPositionInFramesUnlocked();
|
|
|
|
|
|
|
|
// The monitor is held to protect all access to member variables. Write()
|
|
|
|
// waits while mBuffer is full; DataCallback() notifies as it consumes
|
|
|
|
// data from mBuffer. Drain() waits while mState is DRAINING;
|
|
|
|
// StateCallback() notifies when mState is DRAINED.
|
|
|
|
Monitor mMonitor;
|
|
|
|
|
|
|
|
// Sum of silent frames written when DataCallback requests more frames
|
|
|
|
// than are available in mBuffer.
|
|
|
|
PRUint64 mLostFrames;
|
|
|
|
|
|
|
|
// Temporary audio buffer. Filled by Write() and consumed by
|
2012-05-03 04:48:54 +00:00
|
|
|
// DataCallback(). Once mBuffer is full, Write() blocks until sufficient
|
|
|
|
// space becomes available in mBuffer. mBuffer is sized in bytes, not
|
|
|
|
// frames.
|
|
|
|
nsCircularByteBuffer mBuffer;
|
2012-01-12 21:20:36 +00:00
|
|
|
|
|
|
|
// Software volume level. Applied during the servicing of DataCallback().
|
|
|
|
double mVolume;
|
|
|
|
|
|
|
|
// Owning reference to a cubeb_stream. cubeb_stream_destroy is called by
|
|
|
|
// nsAutoRef's destructor.
|
|
|
|
nsAutoRef<cubeb_stream> mCubebStream;
|
|
|
|
|
|
|
|
PRUint32 mBytesPerFrame;
|
|
|
|
|
|
|
|
enum StreamState {
|
|
|
|
INITIALIZED, // Initialized, playback has not begun.
|
|
|
|
STARTED, // Started by a call to Write() (iff INITIALIZED) or Resume().
|
|
|
|
STOPPED, // Stopped by a call to Pause().
|
|
|
|
DRAINING, // Drain requested. DataCallback will indicate end of stream
|
|
|
|
// once the remaining contents of mBuffer are requested by
|
|
|
|
// cubeb, after which StateCallback will indicate drain
|
|
|
|
// completion.
|
2012-04-16 03:00:40 +00:00
|
|
|
DRAINED, // StateCallback has indicated that the drain is complete.
|
|
|
|
ERRORED // Stream disabled due to an internal error.
|
2012-01-12 21:20:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
StreamState mState;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsAudioStream* nsAudioStream::AllocateStream()
|
|
|
|
{
|
|
|
|
#if defined(REMOTE_AUDIO)
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
return new nsRemotedAudioStream();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(MOZ_CUBEB)
|
|
|
|
if (GetUseCubeb()) {
|
|
|
|
return new nsBufferedAudioStream();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return new nsNativeAudioStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MOZ_CUBEB)
|
|
|
|
nsBufferedAudioStream::nsBufferedAudioStream()
|
2012-02-14 04:39:33 +00:00
|
|
|
: mMonitor("nsBufferedAudioStream"), mLostFrames(0), mVolume(1.0),
|
2012-01-12 21:20:36 +00:00
|
|
|
mBytesPerFrame(0), mState(INITIALIZED)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsBufferedAudioStream::~nsBufferedAudioStream()
|
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS0(nsBufferedAudioStream)
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsBufferedAudioStream::Init(PRInt32 aNumChannels, PRInt32 aRate, SampleFormat aFormat)
|
|
|
|
{
|
2012-04-16 03:00:12 +00:00
|
|
|
cubeb* cubebContext = GetCubebContext();
|
|
|
|
|
|
|
|
if (!cubebContext || aNumChannels < 0 || aRate < 0) {
|
2012-01-12 21:20:36 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mRate = aRate;
|
|
|
|
mChannels = aNumChannels;
|
|
|
|
mFormat = aFormat;
|
|
|
|
|
|
|
|
cubeb_stream_params params;
|
|
|
|
params.rate = aRate;
|
|
|
|
params.channels = aNumChannels;
|
|
|
|
switch (aFormat) {
|
|
|
|
case FORMAT_S16_LE:
|
|
|
|
params.format = CUBEB_SAMPLE_S16LE;
|
|
|
|
mBytesPerFrame = sizeof(short) * aNumChannels;
|
|
|
|
break;
|
|
|
|
case FORMAT_FLOAT32:
|
|
|
|
params.format = CUBEB_SAMPLE_FLOAT32LE;
|
|
|
|
mBytesPerFrame = sizeof(float) * aNumChannels;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
cubeb_stream* stream;
|
2012-04-16 03:00:12 +00:00
|
|
|
if (cubeb_stream_init(cubebContext, &stream, "nsBufferedAudioStream", params,
|
2012-05-03 04:48:54 +00:00
|
|
|
GetCubebLatency(), DataCallback_S, StateCallback_S, this) == CUBEB_OK) {
|
2012-01-12 21:20:36 +00:00
|
|
|
mCubebStream.own(stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mCubebStream) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-05-03 04:48:54 +00:00
|
|
|
// Size mBuffer for one second of audio. This value is arbitrary, and was
|
2012-01-12 21:20:36 +00:00
|
|
|
// selected based on the observed behaviour of the existing nsAudioStream
|
|
|
|
// implementations.
|
2012-05-03 04:48:54 +00:00
|
|
|
PRUint32 bufferLimit = aRate * mBytesPerFrame;
|
|
|
|
NS_ABORT_IF_FALSE(bufferLimit % mBytesPerFrame == 0, "Must buffer complete frames");
|
|
|
|
mBuffer.SetCapacity(bufferLimit);
|
2012-01-12 21:20:36 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsBufferedAudioStream::Shutdown()
|
|
|
|
{
|
2012-06-01 04:45:01 +00:00
|
|
|
if (mState == STARTED) {
|
|
|
|
Pause();
|
|
|
|
}
|
2012-01-12 21:20:36 +00:00
|
|
|
if (mCubebStream) {
|
|
|
|
mCubebStream.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsBufferedAudioStream::Write(const void* aBuf, PRUint32 aFrames)
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
2012-04-16 03:00:40 +00:00
|
|
|
if (!mCubebStream || mState == ERRORED) {
|
2012-01-12 21:20:36 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(mState == INITIALIZED || mState == STARTED, "Stream write in unexpected state.");
|
|
|
|
|
|
|
|
const PRUint8* src = static_cast<const PRUint8*>(aBuf);
|
|
|
|
PRUint32 bytesToCopy = aFrames * mBytesPerFrame;
|
|
|
|
|
|
|
|
while (bytesToCopy > 0) {
|
2012-05-03 04:48:54 +00:00
|
|
|
PRUint32 available = NS_MIN(bytesToCopy, mBuffer.Available());
|
2012-01-12 21:20:36 +00:00
|
|
|
NS_ABORT_IF_FALSE(available % mBytesPerFrame == 0, "Must copy complete frames.");
|
|
|
|
|
|
|
|
mBuffer.AppendElements(src, available);
|
|
|
|
src += available;
|
|
|
|
bytesToCopy -= available;
|
|
|
|
|
2012-06-01 04:45:01 +00:00
|
|
|
if (mState != STARTED) {
|
|
|
|
int r;
|
|
|
|
{
|
|
|
|
MonitorAutoUnlock mon(mMonitor);
|
|
|
|
r = cubeb_stream_start(mCubebStream);
|
|
|
|
}
|
|
|
|
mState = r == CUBEB_OK ? STARTED : ERRORED;
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
2012-04-16 03:00:40 +00:00
|
|
|
|
|
|
|
if (mState != STARTED) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-06-01 04:45:01 +00:00
|
|
|
|
|
|
|
if (bytesToCopy > 0) {
|
|
|
|
mon.Wait();
|
|
|
|
}
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32
|
|
|
|
nsBufferedAudioStream::Available()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
NS_ABORT_IF_FALSE(mBuffer.Length() % mBytesPerFrame == 0, "Buffer invariant violated.");
|
2012-05-03 04:48:54 +00:00
|
|
|
return mBuffer.Available() / mBytesPerFrame;
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
|
|
|
|
2012-06-01 04:45:01 +00:00
|
|
|
PRInt32
|
|
|
|
nsBufferedAudioStream::GetMinWriteSize()
|
2012-01-12 21:20:36 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsBufferedAudioStream::SetVolume(double aVolume)
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
NS_ABORT_IF_FALSE(aVolume >= 0.0 && aVolume <= 1.0, "Invalid volume");
|
|
|
|
mVolume = aVolume;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsBufferedAudioStream::Drain()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
if (mState != STARTED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mState = DRAINING;
|
2012-04-16 03:00:40 +00:00
|
|
|
while (mState == DRAINING) {
|
2012-01-12 21:20:36 +00:00
|
|
|
mon.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsBufferedAudioStream::Pause()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
if (!mCubebStream || mState != STARTED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-01 04:45:01 +00:00
|
|
|
int r;
|
|
|
|
{
|
|
|
|
MonitorAutoUnlock mon(mMonitor);
|
|
|
|
r = cubeb_stream_stop(mCubebStream);
|
|
|
|
}
|
|
|
|
if (mState != ERRORED && r == CUBEB_OK) {
|
2012-01-12 21:20:36 +00:00
|
|
|
mState = STOPPED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsBufferedAudioStream::Resume()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
if (!mCubebStream || mState != STOPPED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-01 04:45:01 +00:00
|
|
|
int r;
|
|
|
|
{
|
|
|
|
MonitorAutoUnlock mon(mMonitor);
|
|
|
|
r = cubeb_stream_start(mCubebStream);
|
|
|
|
}
|
|
|
|
if (mState != ERRORED && r == CUBEB_OK) {
|
2012-01-12 21:20:36 +00:00
|
|
|
mState = STARTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-01 04:45:01 +00:00
|
|
|
PRInt64
|
|
|
|
nsBufferedAudioStream::GetPosition()
|
2012-01-12 21:20:36 +00:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
PRInt64 frames = GetPositionInFramesUnlocked();
|
|
|
|
if (frames >= 0) {
|
|
|
|
return USECS_PER_S * frames / mRate;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-07-16 21:21:04 +00:00
|
|
|
// This function is miscompiled by PGO with MSVC 2010. See bug 768333.
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma optimize("", off)
|
|
|
|
#endif
|
2012-01-12 21:20:36 +00:00
|
|
|
PRInt64
|
|
|
|
nsBufferedAudioStream::GetPositionInFrames()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
return GetPositionInFramesUnlocked();
|
|
|
|
}
|
2012-07-16 21:21:04 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma optimize("", on)
|
|
|
|
#endif
|
2012-01-12 21:20:36 +00:00
|
|
|
|
|
|
|
PRInt64
|
|
|
|
nsBufferedAudioStream::GetPositionInFramesUnlocked()
|
|
|
|
{
|
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
|
|
|
|
2012-04-16 03:00:40 +00:00
|
|
|
if (!mCubebStream || mState == ERRORED) {
|
2012-01-12 21:20:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t position = 0;
|
2012-06-01 04:45:01 +00:00
|
|
|
{
|
|
|
|
MonitorAutoUnlock mon(mMonitor);
|
|
|
|
if (cubeb_stream_get_position(mCubebStream, &position) != CUBEB_OK) {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust the reported position by the number of silent frames written
|
|
|
|
// during stream underruns.
|
2012-07-16 21:21:04 +00:00
|
|
|
PRUint64 adjustedPosition = 0;
|
2012-01-12 21:20:36 +00:00
|
|
|
if (position >= mLostFrames) {
|
|
|
|
adjustedPosition = position - mLostFrames;
|
|
|
|
}
|
2012-07-16 21:21:04 +00:00
|
|
|
return NS_MIN<PRUint64>(adjustedPosition, PR_INT64_MAX);
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsBufferedAudioStream::IsPaused()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
return mState == STOPPED;
|
|
|
|
}
|
|
|
|
|
|
|
|
long
|
|
|
|
nsBufferedAudioStream::DataCallback(void* aBuffer, long aFrames)
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
PRUint32 bytesWanted = aFrames * mBytesPerFrame;
|
|
|
|
|
|
|
|
// Adjust bytesWanted to fit what is available in mBuffer.
|
|
|
|
PRUint32 available = NS_MIN(bytesWanted, mBuffer.Length());
|
|
|
|
NS_ABORT_IF_FALSE(available % mBytesPerFrame == 0, "Must copy complete frames");
|
|
|
|
|
2012-05-03 04:48:54 +00:00
|
|
|
if (available > 0) {
|
|
|
|
// Copy each sample from mBuffer to aBuffer, adjusting the volume during the copy.
|
|
|
|
double scaled_volume = GetVolumeScale() * mVolume;
|
2012-01-12 21:20:36 +00:00
|
|
|
|
2012-05-03 04:48:54 +00:00
|
|
|
// Fetch input pointers from the ring buffer.
|
|
|
|
void* input[2];
|
|
|
|
PRUint32 input_size[2];
|
|
|
|
mBuffer.PopElements(available, &input[0], &input_size[0], &input[1], &input_size[1]);
|
|
|
|
|
|
|
|
PRUint8* output = reinterpret_cast<PRUint8*>(aBuffer);
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
// Fast path for unity volume case.
|
|
|
|
if (scaled_volume == 1.0) {
|
|
|
|
memcpy(output, input[i], input_size[i]);
|
|
|
|
output += input_size[i];
|
|
|
|
} else {
|
|
|
|
// Adjust volume as each sample is copied out.
|
|
|
|
switch (mFormat) {
|
|
|
|
case FORMAT_S16_LE: {
|
|
|
|
PRInt32 volume = PRInt32(1 << 16) * scaled_volume;
|
|
|
|
|
|
|
|
const short* src = static_cast<const short*>(input[i]);
|
|
|
|
short* dst = reinterpret_cast<short*>(output);
|
|
|
|
for (PRUint32 j = 0; j < input_size[i] / (mBytesPerFrame / mChannels); ++j) {
|
|
|
|
dst[j] = short((PRInt32(src[j]) * volume) >> 16);
|
|
|
|
}
|
|
|
|
output += input_size[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FORMAT_FLOAT32: {
|
|
|
|
const float* src = static_cast<const float*>(input[i]);
|
|
|
|
float* dst = reinterpret_cast<float*>(output);
|
|
|
|
for (PRUint32 j = 0; j < input_size[i] / (mBytesPerFrame / mChannels); ++j) {
|
|
|
|
dst[j] = src[j] * scaled_volume;
|
|
|
|
}
|
|
|
|
output += input_size[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ABORT_IF_FALSE(mBuffer.Length() % mBytesPerFrame == 0, "Must copy complete frames");
|
2012-01-12 21:20:36 +00:00
|
|
|
|
2012-05-03 04:48:54 +00:00
|
|
|
// Notify any blocked Write() call that more space is available in mBuffer.
|
|
|
|
mon.NotifyAll();
|
|
|
|
|
|
|
|
// Calculate remaining bytes requested by caller. If the stream is not
|
|
|
|
// draining an underrun has occurred, so fill the remaining buffer with
|
|
|
|
// silence.
|
|
|
|
bytesWanted -= available;
|
|
|
|
}
|
2012-01-12 21:20:36 +00:00
|
|
|
|
|
|
|
if (mState != DRAINING) {
|
|
|
|
memset(static_cast<PRUint8*>(aBuffer) + available, 0, bytesWanted);
|
|
|
|
mLostFrames += bytesWanted / mBytesPerFrame;
|
|
|
|
bytesWanted = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aFrames - (bytesWanted / mBytesPerFrame);
|
|
|
|
}
|
|
|
|
|
2012-07-16 21:15:24 +00:00
|
|
|
void
|
2012-01-12 21:20:36 +00:00
|
|
|
nsBufferedAudioStream::StateCallback(cubeb_state aState)
|
|
|
|
{
|
2012-06-01 04:45:01 +00:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
2012-01-12 21:20:36 +00:00
|
|
|
if (aState == CUBEB_STATE_DRAINED) {
|
|
|
|
mState = DRAINED;
|
2012-04-16 03:00:40 +00:00
|
|
|
} else if (aState == CUBEB_STATE_ERROR) {
|
|
|
|
mState = ERRORED;
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
2012-06-01 04:45:01 +00:00
|
|
|
mon.NotifyAll();
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|