mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-15 04:39:31 +00:00
Bug 1198588 - Remove unused MSG-specific code from AudioStream. r=padenot
This commit is contained in:
parent
1d59f8d210
commit
dbad1f30f8
@ -15,12 +15,8 @@
|
||||
#include "mozilla/Snprintf.h"
|
||||
#include <algorithm>
|
||||
#include "mozilla/Telemetry.h"
|
||||
#include "Latency.h"
|
||||
#include "CubebUtils.h"
|
||||
#include "nsPrintfCString.h"
|
||||
#ifdef XP_MACOSX
|
||||
#include <sys/sysctl.h>
|
||||
#endif
|
||||
|
||||
namespace mozilla {
|
||||
|
||||
@ -130,18 +126,11 @@ AudioStream::AudioStream()
|
||||
, mWritten(0)
|
||||
, mAudioClock(this)
|
||||
, mTimeStretcher(nullptr)
|
||||
, mLatencyRequest(HighLatency)
|
||||
, mReadPoint(0)
|
||||
, mDumpFile(nullptr)
|
||||
, mBytesPerFrame(0)
|
||||
, mState(INITIALIZED)
|
||||
, mNeedsStart(false)
|
||||
, mShouldDropFrames(false)
|
||||
, mPendingAudioInitTask(false)
|
||||
, mLastGoodPosition(0)
|
||||
{
|
||||
// keep a ref in case we shut down later than nsLayoutStatics
|
||||
mLatencyLog = AsyncLatencyLogger::Get(true);
|
||||
}
|
||||
|
||||
AudioStream::~AudioStream()
|
||||
@ -164,10 +153,8 @@ AudioStream::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
||||
|
||||
// Possibly add in the future:
|
||||
// - mTimeStretcher
|
||||
// - mLatencyLog
|
||||
// - mCubebStream
|
||||
|
||||
amount += mInserts.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
||||
amount += mBuffer.SizeOfExcludingThis(aMallocSizeOf);
|
||||
|
||||
return amount;
|
||||
@ -319,12 +306,9 @@ WriteDumpFile(FILE* aDumpFile, AudioStream* aStream, uint32_t aFrames,
|
||||
fflush(aDumpFile);
|
||||
}
|
||||
|
||||
// NOTE: this must not block a LowLatency stream for any significant amount
|
||||
// of time, or it will block the entirety of MSG
|
||||
nsresult
|
||||
AudioStream::Init(int32_t aNumChannels, int32_t aRate,
|
||||
const dom::AudioChannel aAudioChannel,
|
||||
LatencyRequest aLatencyRequest)
|
||||
const dom::AudioChannel aAudioChannel)
|
||||
{
|
||||
mStartTime = TimeStamp::Now();
|
||||
mIsFirst = CubebUtils::GetFirstStream();
|
||||
@ -338,7 +322,6 @@ AudioStream::Init(int32_t aNumChannels, int32_t aRate,
|
||||
mInRate = mOutRate = aRate;
|
||||
mChannels = aNumChannels;
|
||||
mOutChannels = (aNumChannels > 2) ? 2 : aNumChannels;
|
||||
mLatencyRequest = aLatencyRequest;
|
||||
|
||||
mDumpFile = OpenDumpFile(this);
|
||||
|
||||
@ -374,108 +357,13 @@ AudioStream::Init(int32_t aNumChannels, int32_t aRate,
|
||||
MOZ_ASSERT(bufferLimit % mBytesPerFrame == 0, "Must buffer complete frames");
|
||||
mBuffer.SetCapacity(bufferLimit);
|
||||
|
||||
if (aLatencyRequest == LowLatency) {
|
||||
// Don't block this thread to initialize a cubeb stream.
|
||||
// When this is done, it will start callbacks from Cubeb. Those will
|
||||
// cause us to move from INITIALIZED to RUNNING. Until then, we
|
||||
// can't access any cubeb functions.
|
||||
// Use a RefPtr to avoid leaks if Dispatch fails
|
||||
mPendingAudioInitTask = true;
|
||||
RefPtr<AudioInitTask> init = new AudioInitTask(this, aLatencyRequest, params);
|
||||
nsresult rv = init->Dispatch();
|
||||
if (NS_FAILED(rv)) {
|
||||
mPendingAudioInitTask = false;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
// High latency - open synchronously
|
||||
nsresult rv = OpenCubeb(params, aLatencyRequest);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
// See if we need to start() the stream, since we must do that from this
|
||||
// thread for now (cubeb API issue)
|
||||
{
|
||||
MonitorAutoLock mon(mMonitor);
|
||||
CheckForStart();
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// On certain MacBookPro, the microphone is located near the left speaker.
|
||||
// We need to pan the sound output to the right speaker if we are using the mic
|
||||
// and the built-in speaker, or we will have terrible echo.
|
||||
void AudioStream::PanOutputIfNeeded(bool aMicrophoneActive)
|
||||
{
|
||||
#ifdef XP_MACOSX
|
||||
cubeb_device* device;
|
||||
int rv;
|
||||
char name[128];
|
||||
size_t length = sizeof(name);
|
||||
bool panCenter = false;
|
||||
|
||||
rv = sysctlbyname("hw.model", name, &length, NULL, 0);
|
||||
if (rv) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!strncmp(name, "MacBookPro", 10)) {
|
||||
if (cubeb_stream_get_current_device(mCubebStream.get(), &device) == CUBEB_OK) {
|
||||
// Check if we are currently outputing sound on external speakers.
|
||||
if (!strcmp(device->output_name, "ispk")) {
|
||||
// Pan everything to the right speaker.
|
||||
if (aMicrophoneActive) {
|
||||
LOG(("%p Panning audio output to the right.", this));
|
||||
if (cubeb_stream_set_panning(mCubebStream.get(), 1.0) != CUBEB_OK) {
|
||||
NS_WARNING("Could not pan audio output to the right.");
|
||||
}
|
||||
} else {
|
||||
panCenter = true;
|
||||
}
|
||||
} else {
|
||||
panCenter = true;
|
||||
}
|
||||
if (panCenter) {
|
||||
LOG(("%p Panning audio output to the center.", this));
|
||||
if (cubeb_stream_set_panning(mCubebStream.get(), 0.0) != CUBEB_OK) {
|
||||
NS_WARNING("Could not pan audio output to the center.");
|
||||
}
|
||||
}
|
||||
cubeb_stream_device_destroy(mCubebStream.get(), device);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void AudioStream::ResetStreamIfNeeded()
|
||||
{
|
||||
cubeb_device * device;
|
||||
// Only reset a device if a mic is active, and this is a low latency stream.
|
||||
if (!mMicrophoneActive || mLatencyRequest != LowLatency) {
|
||||
return;
|
||||
}
|
||||
if (cubeb_stream_get_current_device(mCubebStream.get(), &device) == CUBEB_OK) {
|
||||
// This a microphone that goes through the headphone plug, reset the
|
||||
// output to prevent echo building up.
|
||||
if (strcmp(device->input_name, "emic") == 0) {
|
||||
LOG(("Resetting audio output"));
|
||||
Reset();
|
||||
}
|
||||
cubeb_stream_device_destroy(mCubebStream.get(), device);
|
||||
}
|
||||
}
|
||||
|
||||
void AudioStream::DeviceChangedCallback()
|
||||
{
|
||||
MonitorAutoLock mon(mMonitor);
|
||||
PanOutputIfNeeded(mMicrophoneActive);
|
||||
mShouldDropFrames = true;
|
||||
ResetStreamIfNeeded();
|
||||
return OpenCubeb(params);
|
||||
}
|
||||
|
||||
// This code used to live inside AudioStream::Init(), but on Mac (others?)
|
||||
// it has been known to take 300-800 (or even 8500) ms to execute(!)
|
||||
nsresult
|
||||
AudioStream::OpenCubeb(cubeb_stream_params &aParams,
|
||||
LatencyRequest aLatencyRequest)
|
||||
AudioStream::OpenCubeb(cubeb_stream_params &aParams)
|
||||
{
|
||||
cubeb* cubebContext = CubebUtils::GetCubebContext();
|
||||
if (!cubebContext) {
|
||||
@ -488,14 +376,7 @@ AudioStream::OpenCubeb(cubeb_stream_params &aParams,
|
||||
// If the latency pref is set, use it. Otherwise, if this stream is intended
|
||||
// for low latency playback, try to get the lowest latency possible.
|
||||
// Otherwise, for normal streams, use 100ms.
|
||||
uint32_t latency;
|
||||
if (aLatencyRequest == LowLatency && !CubebUtils::CubebLatencyPrefSet()) {
|
||||
if (cubeb_get_min_latency(cubebContext, aParams, &latency) != CUBEB_OK) {
|
||||
latency = CubebUtils::GetCubebLatency();
|
||||
}
|
||||
} else {
|
||||
latency = CubebUtils::GetCubebLatency();
|
||||
}
|
||||
uint32_t latency = CubebUtils::GetCubebLatency();
|
||||
|
||||
{
|
||||
cubeb_stream* stream;
|
||||
@ -504,9 +385,6 @@ AudioStream::OpenCubeb(cubeb_stream_params &aParams,
|
||||
MonitorAutoLock mon(mMonitor);
|
||||
MOZ_ASSERT(mState != SHUTDOWN);
|
||||
mCubebStream.reset(stream);
|
||||
// We can't cubeb_stream_start() the thread from a transient thread due to
|
||||
// cubeb API requirements (init can be called from another thread, but
|
||||
// not start/stop/destroy/etc)
|
||||
} else {
|
||||
MonitorAutoLock mon(mMonitor);
|
||||
mState = ERRORED;
|
||||
@ -515,9 +393,6 @@ AudioStream::OpenCubeb(cubeb_stream_params &aParams,
|
||||
}
|
||||
}
|
||||
|
||||
cubeb_stream_register_device_changed_callback(mCubebStream.get(),
|
||||
AudioStream::DeviceChangedCallback_s);
|
||||
|
||||
mState = INITIALIZED;
|
||||
|
||||
if (!mStartTime.IsNull()) {
|
||||
@ -531,72 +406,12 @@ AudioStream::OpenCubeb(cubeb_stream_params &aParams,
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void
|
||||
AudioStream::AudioInitTaskFinished()
|
||||
{
|
||||
MonitorAutoLock mon(mMonitor);
|
||||
mPendingAudioInitTask = false;
|
||||
mon.NotifyAll();
|
||||
}
|
||||
|
||||
void
|
||||
AudioStream::CheckForStart()
|
||||
{
|
||||
mMonitor.AssertCurrentThreadOwns();
|
||||
if (mState == INITIALIZED) {
|
||||
// Start the stream right away when low latency has been requested. This means
|
||||
// that the DataCallback will feed silence to cubeb, until the first frames
|
||||
// are written to this AudioStream. Also start if a start has been queued.
|
||||
if (mLatencyRequest == LowLatency || mNeedsStart) {
|
||||
StartUnlocked(); // mState = STARTED or ERRORED
|
||||
mNeedsStart = false;
|
||||
MOZ_LOG(gAudioStreamLog, LogLevel::Warning,
|
||||
("Started waiting %s-latency stream",
|
||||
mLatencyRequest == LowLatency ? "low" : "high"));
|
||||
} else {
|
||||
// high latency, not full - OR Pause() was called before we got here
|
||||
MOZ_LOG(gAudioStreamLog, LogLevel::Debug,
|
||||
("Not starting waiting %s-latency stream",
|
||||
mLatencyRequest == LowLatency ? "low" : "high"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
AudioInitTask::Run()
|
||||
{
|
||||
MOZ_ASSERT(mThread);
|
||||
if (NS_IsMainThread()) {
|
||||
mThread->Shutdown(); // can't Shutdown from the thread itself, darn
|
||||
// Don't null out mThread!
|
||||
// See bug 999104. We must hold a ref to the thread across Dispatch()
|
||||
// since the internal mThread ref could be released while processing
|
||||
// the Dispatch(), and Dispatch/PutEvent itself doesn't hold a ref; it
|
||||
// assumes the caller does.
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult rv = mAudioStream->OpenCubeb(mParams, mLatencyRequest);
|
||||
mAudioStream->AudioInitTaskFinished();
|
||||
|
||||
// and now kill this thread
|
||||
NS_DispatchToMainThread(this);
|
||||
return rv;
|
||||
}
|
||||
|
||||
// aTime is the time in ms the samples were inserted into MediaStreamGraph
|
||||
nsresult
|
||||
AudioStream::Write(const AudioDataValue* aBuf, uint32_t aFrames, TimeStamp *aTime)
|
||||
AudioStream::Write(const AudioDataValue* aBuf, uint32_t aFrames)
|
||||
{
|
||||
MonitorAutoLock mon(mMonitor);
|
||||
|
||||
// See if we need to start() the stream, since we must do that from this thread
|
||||
CheckForStart();
|
||||
|
||||
if (mShouldDropFrames) {
|
||||
mBuffer.ContractTo(0);
|
||||
return NS_OK;
|
||||
}
|
||||
if (mState == ERRORED) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
@ -614,22 +429,6 @@ AudioStream::Write(const AudioDataValue* aBuf, uint32_t aFrames, TimeStamp *aTim
|
||||
const uint8_t* src = reinterpret_cast<const uint8_t*>(aBuf);
|
||||
uint32_t bytesToCopy = FramesToBytes(aFrames);
|
||||
|
||||
// XXX this will need to change if we want to enable this on-the-fly!
|
||||
if (MOZ_LOG_TEST(GetLatencyLog(), LogLevel::Debug)) {
|
||||
// Record the position and time this data was inserted
|
||||
int64_t timeMs;
|
||||
if (aTime && !aTime->IsNull()) {
|
||||
if (mStartTime.IsNull()) {
|
||||
AsyncLatencyLogger::Get(true)->GetStartTime(mStartTime);
|
||||
}
|
||||
timeMs = (*aTime - mStartTime).ToMilliseconds();
|
||||
} else {
|
||||
timeMs = 0;
|
||||
}
|
||||
struct Inserts insert = { timeMs, aFrames};
|
||||
mInserts.AppendElement(insert);
|
||||
}
|
||||
|
||||
while (bytesToCopy > 0) {
|
||||
uint32_t available = std::min(bytesToCopy, mBuffer.Available());
|
||||
MOZ_ASSERT(available % mBytesPerFrame == 0,
|
||||
@ -640,33 +439,19 @@ AudioStream::Write(const AudioDataValue* aBuf, uint32_t aFrames, TimeStamp *aTim
|
||||
bytesToCopy -= available;
|
||||
|
||||
if (bytesToCopy > 0) {
|
||||
// Careful - the CubebInit thread may not have gotten to STARTED yet
|
||||
if ((mState == INITIALIZED || mState == STARTED) && mLatencyRequest == LowLatency) {
|
||||
// don't ever block MediaStreamGraph low-latency streams
|
||||
uint32_t remains = 0; // we presume the buffer is full
|
||||
if (mBuffer.Length() > bytesToCopy) {
|
||||
remains = mBuffer.Length() - bytesToCopy; // Free up just enough space
|
||||
}
|
||||
// account for dropping samples
|
||||
MOZ_LOG(gAudioStreamLog, LogLevel::Warning, ("Stream %p dropping %u bytes (%u frames)in Write()",
|
||||
this, mBuffer.Length() - remains, BytesToFrames(mBuffer.Length() - remains)));
|
||||
mReadPoint += BytesToFrames(mBuffer.Length() - remains);
|
||||
mBuffer.ContractTo(remains);
|
||||
} else { // RUNNING or high latency
|
||||
// If we are not playing, but our buffer is full, start playing to make
|
||||
// room for soon-to-be-decoded data.
|
||||
if (mState != STARTED && mState != RUNNING) {
|
||||
MOZ_LOG(gAudioStreamLog, LogLevel::Warning, ("Starting stream %p in Write (%u waiting)",
|
||||
this, bytesToCopy));
|
||||
StartUnlocked();
|
||||
if (mState == ERRORED) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
}
|
||||
MOZ_LOG(gAudioStreamLog, LogLevel::Warning, ("Stream %p waiting in Write() (%u waiting)",
|
||||
this, bytesToCopy));
|
||||
mon.Wait();
|
||||
}
|
||||
// If we are not playing, but our buffer is full, start playing to make
|
||||
// room for soon-to-be-decoded data.
|
||||
if (mState != STARTED && mState != RUNNING) {
|
||||
MOZ_LOG(gAudioStreamLog, LogLevel::Warning, ("Starting stream %p in Write (%u waiting)",
|
||||
this, bytesToCopy));
|
||||
StartUnlocked();
|
||||
if (mState == ERRORED) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
}
|
||||
MOZ_LOG(gAudioStreamLog, LogLevel::Warning, ("Stream %p waiting in Write() (%u waiting)",
|
||||
this, bytesToCopy));
|
||||
mon.Wait();
|
||||
}
|
||||
}
|
||||
|
||||
@ -692,16 +477,6 @@ AudioStream::SetVolume(double aVolume)
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
AudioStream::SetMicrophoneActive(bool aActive)
|
||||
{
|
||||
MonitorAutoLock mon(mMonitor);
|
||||
|
||||
mMicrophoneActive = aActive;
|
||||
|
||||
PanOutputIfNeeded(mMicrophoneActive);
|
||||
}
|
||||
|
||||
void
|
||||
AudioStream::Cancel()
|
||||
{
|
||||
@ -737,7 +512,6 @@ AudioStream::StartUnlocked()
|
||||
{
|
||||
mMonitor.AssertCurrentThreadOwns();
|
||||
if (!mCubebStream) {
|
||||
mNeedsStart = true;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -746,8 +520,6 @@ AudioStream::StartUnlocked()
|
||||
{
|
||||
MonitorAutoUnlock mon(mMonitor);
|
||||
r = cubeb_stream_start(mCubebStream.get());
|
||||
|
||||
PanOutputIfNeeded(mMicrophoneActive);
|
||||
}
|
||||
mState = r == CUBEB_OK ? STARTED : ERRORED;
|
||||
LOG(("AudioStream: started %p, state %s", this, mState == STARTED ? "STARTED" : "ERRORED"));
|
||||
@ -764,7 +536,6 @@ AudioStream::Pause()
|
||||
}
|
||||
|
||||
if (!mCubebStream || (mState != STARTED && mState != RUNNING)) {
|
||||
mNeedsStart = false;
|
||||
mState = STOPPED; // which also tells async OpenCubeb not to start, just init
|
||||
return;
|
||||
}
|
||||
@ -803,10 +574,6 @@ AudioStream::Shutdown()
|
||||
MonitorAutoLock mon(mMonitor);
|
||||
LOG(("AudioStream: Shutdown %p, state %d", this, mState));
|
||||
|
||||
while (mPendingAudioInitTask) {
|
||||
mon.Wait();
|
||||
}
|
||||
|
||||
if (mCubebStream) {
|
||||
MonitorAutoUnlock mon(mMonitor);
|
||||
// Force stop to put the cubeb stream in a stable state before deletion.
|
||||
@ -865,17 +632,6 @@ AudioStream::GetPositionInFramesUnlocked()
|
||||
return std::min<uint64_t>(mLastGoodPosition, INT64_MAX);
|
||||
}
|
||||
|
||||
int64_t
|
||||
AudioStream::GetLatencyInFrames()
|
||||
{
|
||||
uint32_t latency;
|
||||
if (cubeb_stream_get_latency(mCubebStream.get(), &latency)) {
|
||||
NS_WARNING("Could not get cubeb latency.");
|
||||
return 0;
|
||||
}
|
||||
return static_cast<int64_t>(latency);
|
||||
}
|
||||
|
||||
bool
|
||||
AudioStream::IsPaused()
|
||||
{
|
||||
@ -883,26 +639,8 @@ AudioStream::IsPaused()
|
||||
return mState == STOPPED;
|
||||
}
|
||||
|
||||
void
|
||||
AudioStream::GetBufferInsertTime(int64_t &aTimeMs)
|
||||
{
|
||||
mMonitor.AssertCurrentThreadOwns();
|
||||
if (mInserts.Length() > 0) {
|
||||
// Find the right block, but don't leave the array empty
|
||||
while (mInserts.Length() > 1 && mReadPoint >= mInserts[0].mFrames) {
|
||||
mReadPoint -= mInserts[0].mFrames;
|
||||
mInserts.RemoveElementAt(0);
|
||||
}
|
||||
// offset for amount already read
|
||||
// XXX Note: could misreport if we couldn't find a block in the right timeframe
|
||||
aTimeMs = mInserts[0].mTimeMs + ((mReadPoint * 1000) / mOutRate);
|
||||
} else {
|
||||
aTimeMs = INT64_MAX;
|
||||
}
|
||||
}
|
||||
|
||||
long
|
||||
AudioStream::GetUnprocessed(void* aBuffer, long aFrames, int64_t &aTimeMs)
|
||||
AudioStream::GetUnprocessed(void* aBuffer, long aFrames)
|
||||
{
|
||||
mMonitor.AssertCurrentThreadOwns();
|
||||
uint8_t* wpos = reinterpret_cast<uint8_t*>(aBuffer);
|
||||
@ -924,42 +662,11 @@ AudioStream::GetUnprocessed(void* aBuffer, long aFrames, int64_t &aTimeMs)
|
||||
wpos += input_size[0];
|
||||
memcpy(wpos, input[1], input_size[1]);
|
||||
|
||||
// First time block now has our first returned sample
|
||||
mReadPoint += BytesToFrames(available);
|
||||
GetBufferInsertTime(aTimeMs);
|
||||
|
||||
return BytesToFrames(available) + flushedFrames;
|
||||
}
|
||||
|
||||
// Get unprocessed samples, and pad the beginning of the buffer with silence if
|
||||
// there is not enough data.
|
||||
long
|
||||
AudioStream::GetUnprocessedWithSilencePadding(void* aBuffer, long aFrames, int64_t& aTimeMs)
|
||||
{
|
||||
mMonitor.AssertCurrentThreadOwns();
|
||||
uint32_t toPopBytes = FramesToBytes(aFrames);
|
||||
uint32_t available = std::min(toPopBytes, mBuffer.Length());
|
||||
uint32_t silenceOffset = toPopBytes - available;
|
||||
|
||||
uint8_t* wpos = reinterpret_cast<uint8_t*>(aBuffer);
|
||||
|
||||
memset(wpos, 0, silenceOffset);
|
||||
wpos += silenceOffset;
|
||||
|
||||
void* input[2];
|
||||
uint32_t input_size[2];
|
||||
mBuffer.PopElements(available, &input[0], &input_size[0], &input[1], &input_size[1]);
|
||||
memcpy(wpos, input[0], input_size[0]);
|
||||
wpos += input_size[0];
|
||||
memcpy(wpos, input[1], input_size[1]);
|
||||
|
||||
GetBufferInsertTime(aTimeMs);
|
||||
|
||||
return aFrames;
|
||||
}
|
||||
|
||||
long
|
||||
AudioStream::GetTimeStretched(void* aBuffer, long aFrames, int64_t &aTimeMs)
|
||||
AudioStream::GetTimeStretched(void* aBuffer, long aFrames)
|
||||
{
|
||||
mMonitor.AssertCurrentThreadOwns();
|
||||
long processedFrames = 0;
|
||||
@ -985,7 +692,6 @@ AudioStream::GetTimeStretched(void* aBuffer, long aFrames, int64_t &aTimeMs)
|
||||
}
|
||||
mBuffer.PopElements(available, &input[0], &input_size[0],
|
||||
&input[1], &input_size[1]);
|
||||
mReadPoint += BytesToFrames(available);
|
||||
for(uint32_t i = 0; i < 2; i++) {
|
||||
mTimeStretcher->putSamples(reinterpret_cast<AudioDataValue*>(input[i]), BytesToFrames(input_size[i]));
|
||||
}
|
||||
@ -995,59 +701,9 @@ AudioStream::GetTimeStretched(void* aBuffer, long aFrames, int64_t &aTimeMs)
|
||||
processedFrames += receivedFrames;
|
||||
} while (processedFrames < aFrames && !lowOnBufferedData);
|
||||
|
||||
GetBufferInsertTime(aTimeMs);
|
||||
|
||||
return processedFrames;
|
||||
}
|
||||
|
||||
void
|
||||
AudioStream::Reset()
|
||||
{
|
||||
|
||||
MOZ_ASSERT(mLatencyRequest == LowLatency, "We should only be reseting low latency streams");
|
||||
|
||||
mShouldDropFrames = true;
|
||||
mNeedsStart = true;
|
||||
|
||||
cubeb_stream_params params;
|
||||
params.rate = mInRate;
|
||||
params.channels = mOutChannels;
|
||||
#if defined(__ANDROID__)
|
||||
#if defined(MOZ_B2G)
|
||||
params.stream_type = CubebUtils::ConvertChannelToCubebType(mAudioChannel);
|
||||
#else
|
||||
params.stream_type = CUBEB_STREAM_TYPE_MUSIC;
|
||||
#endif
|
||||
|
||||
if (params.stream_type == CUBEB_STREAM_TYPE_MAX) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (AUDIO_OUTPUT_FORMAT == AUDIO_FORMAT_S16) {
|
||||
params.format = CUBEB_SAMPLE_S16NE;
|
||||
} else {
|
||||
params.format = CUBEB_SAMPLE_FLOAT32NE;
|
||||
}
|
||||
mBytesPerFrame = sizeof(AudioDataValue) * mOutChannels;
|
||||
|
||||
// Size mBuffer for one second of audio. This value is arbitrary, and was
|
||||
// selected based on the observed behaviour of the existing AudioStream
|
||||
// implementations.
|
||||
uint32_t bufferLimit = FramesToBytes(mInRate);
|
||||
MOZ_ASSERT(bufferLimit % mBytesPerFrame == 0, "Must buffer complete frames");
|
||||
mBuffer.Reset();
|
||||
mBuffer.SetCapacity(bufferLimit);
|
||||
|
||||
// Don't block this thread to initialize a cubeb stream.
|
||||
// When this is done, it will start callbacks from Cubeb. Those will
|
||||
// cause us to move from INITIALIZED to RUNNING. Until then, we
|
||||
// can't access any cubeb functions.
|
||||
// Use a RefPtr to avoid leaks if Dispatch fails
|
||||
RefPtr<AudioInitTask> init = new AudioInitTask(this, mLatencyRequest, params);
|
||||
init->Dispatch();
|
||||
}
|
||||
|
||||
long
|
||||
AudioStream::DataCallback(void* aBuffer, long aFrames)
|
||||
{
|
||||
@ -1058,63 +714,26 @@ AudioStream::DataCallback(void* aBuffer, long aFrames)
|
||||
AudioDataValue* output = reinterpret_cast<AudioDataValue*>(aBuffer);
|
||||
uint32_t underrunFrames = 0;
|
||||
uint32_t servicedFrames = 0;
|
||||
int64_t insertTime;
|
||||
|
||||
mShouldDropFrames = false;
|
||||
|
||||
// NOTE: wasapi (others?) can call us back *after* stop()/Shutdown() (mState == SHUTDOWN)
|
||||
// Bug 996162
|
||||
|
||||
// callback tells us cubeb succeeded initializing
|
||||
if (mState == STARTED) {
|
||||
// For low-latency streams, we want to minimize any built-up data when
|
||||
// we start getting callbacks.
|
||||
// Simple version - contract on first callback only.
|
||||
if (mLatencyRequest == LowLatency) {
|
||||
uint32_t old_len = mBuffer.Length();
|
||||
available = mBuffer.ContractTo(FramesToBytes(aFrames));
|
||||
TimeStamp now = TimeStamp::Now();
|
||||
if (!mStartTime.IsNull()) {
|
||||
int64_t timeMs = (now - mStartTime).ToMilliseconds();
|
||||
MOZ_LOG(gAudioStreamLog, LogLevel::Warning,
|
||||
("Stream took %lldms to start after first Write() @ %u", timeMs, mOutRate));
|
||||
} else {
|
||||
MOZ_LOG(gAudioStreamLog, LogLevel::Warning,
|
||||
("Stream started before Write() @ %u", mOutRate));
|
||||
}
|
||||
|
||||
if (old_len != available) {
|
||||
// Note that we may have dropped samples in Write() as well!
|
||||
MOZ_LOG(gAudioStreamLog, LogLevel::Warning,
|
||||
("AudioStream %p dropped %u + %u initial frames @ %u", this,
|
||||
mReadPoint, BytesToFrames(old_len - available), mOutRate));
|
||||
mReadPoint += BytesToFrames(old_len - available);
|
||||
}
|
||||
}
|
||||
mState = RUNNING;
|
||||
}
|
||||
|
||||
if (available) {
|
||||
// When we are playing a low latency stream, and it is the first time we are
|
||||
// getting data from the buffer, we prefer to add the silence for an
|
||||
// underrun at the beginning of the buffer, so the first buffer is not cut
|
||||
// in half by the silence inserted to compensate for the underrun.
|
||||
if (mInRate == mOutRate) {
|
||||
if (mLatencyRequest == LowLatency && !mWritten) {
|
||||
servicedFrames = GetUnprocessedWithSilencePadding(output, aFrames, insertTime);
|
||||
} else {
|
||||
servicedFrames = GetUnprocessed(output, aFrames, insertTime);
|
||||
}
|
||||
servicedFrames = GetUnprocessed(output, aFrames);
|
||||
} else {
|
||||
servicedFrames = GetTimeStretched(output, aFrames, insertTime);
|
||||
servicedFrames = GetTimeStretched(output, aFrames);
|
||||
}
|
||||
|
||||
MOZ_ASSERT(mBuffer.Length() % mBytesPerFrame == 0, "Must copy complete frames");
|
||||
|
||||
// Notify any blocked Write() call that more space is available in mBuffer.
|
||||
mon.NotifyAll();
|
||||
} else {
|
||||
GetBufferInsertTime(insertTime);
|
||||
}
|
||||
|
||||
underrunFrames = aFrames - servicedFrames;
|
||||
@ -1135,21 +754,6 @@ AudioStream::DataCallback(void* aBuffer, long aFrames)
|
||||
}
|
||||
|
||||
WriteDumpFile(mDumpFile, this, aFrames, aBuffer);
|
||||
// Don't log if we're not interested or if the stream is inactive
|
||||
if (MOZ_LOG_TEST(GetLatencyLog(), LogLevel::Debug) &&
|
||||
mState != SHUTDOWN &&
|
||||
insertTime != INT64_MAX && servicedFrames > underrunFrames) {
|
||||
uint32_t latency = UINT32_MAX;
|
||||
if (cubeb_stream_get_latency(mCubebStream.get(), &latency)) {
|
||||
NS_WARNING("Could not get latency from cubeb.");
|
||||
}
|
||||
TimeStamp now = TimeStamp::Now();
|
||||
|
||||
mLatencyLog->Log(AsyncLatencyLogger::AudioStream, reinterpret_cast<uint64_t>(this),
|
||||
insertTime, now);
|
||||
mLatencyLog->Log(AsyncLatencyLogger::Cubeb, reinterpret_cast<uint64_t>(mCubebStream.get()),
|
||||
(latency * 1000) / mOutRate, now);
|
||||
}
|
||||
|
||||
return servicedFrames;
|
||||
}
|
||||
|
@ -10,9 +10,10 @@
|
||||
#include "nsAutoPtr.h"
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsThreadUtils.h"
|
||||
#include "Latency.h"
|
||||
#include "mozilla/dom/AudioChannelBinding.h"
|
||||
#include "mozilla/Monitor.h"
|
||||
#include "mozilla/RefPtr.h"
|
||||
#include "mozilla/TimeStamp.h"
|
||||
#include "mozilla/UniquePtr.h"
|
||||
#include "CubebUtils.h"
|
||||
#include "soundtouch/SoundTouchFactory.h"
|
||||
@ -71,7 +72,7 @@ private:
|
||||
int mInRate;
|
||||
// True if the we are timestretching, false if we are resampling.
|
||||
bool mPreservesPitch;
|
||||
// The history of frames sent to the audio engine in each Datacallback.
|
||||
// The history of frames sent to the audio engine in each DataCallback.
|
||||
const nsAutoPtr<FrameHistory> mFrameHistory;
|
||||
};
|
||||
|
||||
@ -133,19 +134,6 @@ public:
|
||||
mStart %= mCapacity;
|
||||
}
|
||||
|
||||
// Throw away all but aSize bytes from the buffer. Returns new size, which
|
||||
// may be less than aSize
|
||||
uint32_t ContractTo(uint32_t aSize) {
|
||||
MOZ_ASSERT(mBuffer && mCapacity, "Buffer not initialized.");
|
||||
if (aSize >= mCount) {
|
||||
return mCount;
|
||||
}
|
||||
mStart += (mCount - aSize);
|
||||
mCount = aSize;
|
||||
mStart %= mCapacity;
|
||||
return mCount;
|
||||
}
|
||||
|
||||
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
||||
{
|
||||
size_t amount = 0;
|
||||
@ -153,14 +141,6 @@ public:
|
||||
return amount;
|
||||
}
|
||||
|
||||
void Reset()
|
||||
{
|
||||
mBuffer = nullptr;
|
||||
mCapacity = 0;
|
||||
mStart = 0;
|
||||
mCount = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
nsAutoArrayPtr<uint8_t> mBuffer;
|
||||
uint32_t mCapacity;
|
||||
@ -168,8 +148,6 @@ private:
|
||||
uint32_t mCount;
|
||||
};
|
||||
|
||||
class AudioInitTask;
|
||||
|
||||
// Access to a single instance of this class must be synchronized by
|
||||
// callers, or made from a single thread. One exception is that access to
|
||||
// GetPosition, GetPositionInFrames, SetVolume, and Get{Rate,Channels},
|
||||
@ -182,17 +160,11 @@ public:
|
||||
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AudioStream)
|
||||
AudioStream();
|
||||
|
||||
enum LatencyRequest {
|
||||
HighLatency,
|
||||
LowLatency
|
||||
};
|
||||
|
||||
// Initialize the audio stream. aNumChannels is the number of audio
|
||||
// channels (1 for mono, 2 for stereo, etc) and aRate is the sample rate
|
||||
// (22050Hz, 44100Hz, etc).
|
||||
nsresult Init(int32_t aNumChannels, int32_t aRate,
|
||||
const dom::AudioChannel aAudioStreamChannel,
|
||||
LatencyRequest aLatencyRequest);
|
||||
const dom::AudioChannel aAudioStreamChannel);
|
||||
|
||||
// Closes the stream. All future use of the stream is an error.
|
||||
void Shutdown();
|
||||
@ -202,9 +174,8 @@ public:
|
||||
// Write audio data to the audio hardware. aBuf is an array of AudioDataValues
|
||||
// AudioDataValue of length aFrames*mChannels. If aFrames is larger
|
||||
// than the result of Available(), the write will block until sufficient
|
||||
// buffer space is available. aTime is the time in ms associated with the first sample
|
||||
// for latency calculations
|
||||
nsresult Write(const AudioDataValue* aBuf, uint32_t aFrames, TimeStamp* aTime = nullptr);
|
||||
// buffer space is available.
|
||||
nsresult Write(const AudioDataValue* aBuf, uint32_t aFrames);
|
||||
|
||||
// Return the number of audio frames that can be written without blocking.
|
||||
uint32_t Available();
|
||||
@ -213,12 +184,6 @@ public:
|
||||
// 0 (meaning muted) to 1 (meaning full volume). Thread-safe.
|
||||
void SetVolume(double aVolume);
|
||||
|
||||
// Informs the AudioStream that a microphone is being used by someone in the
|
||||
// application.
|
||||
void SetMicrophoneActive(bool aActive);
|
||||
void PanOutputIfNeeded(bool aMicrophoneActive);
|
||||
void ResetStreamIfNeeded();
|
||||
|
||||
// Block until buffered audio data has been consumed.
|
||||
void Drain();
|
||||
|
||||
@ -271,14 +236,7 @@ protected:
|
||||
int64_t GetPositionInFramesUnlocked();
|
||||
|
||||
private:
|
||||
friend class AudioInitTask;
|
||||
|
||||
// So we can call it asynchronously from AudioInitTask
|
||||
nsresult OpenCubeb(cubeb_stream_params &aParams,
|
||||
LatencyRequest aLatencyRequest);
|
||||
void AudioInitTaskFinished();
|
||||
|
||||
void CheckForStart();
|
||||
nsresult OpenCubeb(cubeb_stream_params &aParams);
|
||||
|
||||
static long DataCallback_S(cubeb_stream*, void* aThis, void* aBuffer, long aFrames)
|
||||
{
|
||||
@ -291,23 +249,13 @@ private:
|
||||
}
|
||||
|
||||
|
||||
static void DeviceChangedCallback_s(void * aThis) {
|
||||
static_cast<AudioStream*>(aThis)->DeviceChangedCallback();
|
||||
}
|
||||
|
||||
long DataCallback(void* aBuffer, long aFrames);
|
||||
void StateCallback(cubeb_state aState);
|
||||
void DeviceChangedCallback();
|
||||
|
||||
nsresult EnsureTimeStretcherInitializedUnlocked();
|
||||
|
||||
// aTime is the time in ms the samples were inserted into MediaStreamGraph
|
||||
long GetUnprocessed(void* aBuffer, long aFrames, int64_t &aTime);
|
||||
long GetTimeStretched(void* aBuffer, long aFrames, int64_t &aTime);
|
||||
long GetUnprocessedWithSilencePadding(void* aBuffer, long aFrames, int64_t &aTime);
|
||||
|
||||
int64_t GetLatencyInFrames();
|
||||
void GetBufferInsertTime(int64_t &aTimeMs);
|
||||
long GetUnprocessed(void* aBuffer, long aFrames);
|
||||
long GetTimeStretched(void* aBuffer, long aFrames);
|
||||
|
||||
void StartUnlocked();
|
||||
|
||||
@ -330,22 +278,9 @@ private:
|
||||
int64_t mWritten;
|
||||
AudioClock mAudioClock;
|
||||
soundtouch::SoundTouch* mTimeStretcher;
|
||||
nsRefPtr<AsyncLatencyLogger> mLatencyLog;
|
||||
|
||||
// copy of Latency logger's starting time for offset calculations
|
||||
// Stream start time for stream open delay telemetry.
|
||||
TimeStamp mStartTime;
|
||||
// Whether we are playing a low latency stream, or a normal stream.
|
||||
LatencyRequest mLatencyRequest;
|
||||
// Where in the current mInserts[0] block cubeb has read to
|
||||
int64_t mReadPoint;
|
||||
// Keep track of each inserted block of samples and the time it was inserted
|
||||
// so we can estimate the clock time for a specific sample's insertion (for when
|
||||
// we send data to cubeb). Blocks are aged out as needed.
|
||||
struct Inserts {
|
||||
int64_t mTimeMs;
|
||||
int64_t mFrames;
|
||||
};
|
||||
nsAutoTArray<Inserts, 8> mInserts;
|
||||
|
||||
// Output file for dumping audio
|
||||
FILE* mDumpFile;
|
||||
@ -386,57 +321,12 @@ private:
|
||||
};
|
||||
|
||||
StreamState mState;
|
||||
bool mNeedsStart; // needed in case Start() is called before cubeb is open
|
||||
bool mIsFirst;
|
||||
// True if a microphone is active.
|
||||
bool mMicrophoneActive;
|
||||
// When we are in the process of changing the output device, and the callback
|
||||
// is not going to be called for a little while, simply drop incoming frames.
|
||||
// This is only on OSX for now, because other systems handle this gracefully.
|
||||
bool mShouldDropFrames;
|
||||
// True if there is a pending AudioInitTask. Shutdown() will wait until the
|
||||
// pending AudioInitTask is finished.
|
||||
bool mPendingAudioInitTask;
|
||||
// The last good position returned by cubeb_stream_get_position(). Used to
|
||||
// check if the cubeb position is going backward.
|
||||
uint64_t mLastGoodPosition;
|
||||
};
|
||||
|
||||
class AudioInitTask : public nsRunnable
|
||||
{
|
||||
public:
|
||||
AudioInitTask(AudioStream *aStream,
|
||||
AudioStream::LatencyRequest aLatencyRequest,
|
||||
const cubeb_stream_params &aParams)
|
||||
: mAudioStream(aStream)
|
||||
, mLatencyRequest(aLatencyRequest)
|
||||
, mParams(aParams)
|
||||
{}
|
||||
|
||||
nsresult Dispatch()
|
||||
{
|
||||
// Can't add 'this' as the event to run, since mThread may not be set yet
|
||||
nsresult rv = NS_NewNamedThread("CubebInit", getter_AddRefs(mThread));
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
// Note: event must not null out mThread!
|
||||
rv = mThread->Dispatch(this, NS_DISPATCH_NORMAL);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual ~AudioInitTask() {};
|
||||
|
||||
private:
|
||||
NS_IMETHOD Run() override final;
|
||||
|
||||
RefPtr<AudioStream> mAudioStream;
|
||||
AudioStream::LatencyRequest mLatencyRequest;
|
||||
cubeb_stream_params mParams;
|
||||
|
||||
nsCOMPtr<nsIThread> mThread;
|
||||
};
|
||||
|
||||
} // namespace mozilla
|
||||
|
||||
#endif
|
||||
|
@ -263,8 +263,7 @@ DecodedAudioDataSink::InitializeAudioStream()
|
||||
// circumstances, so we take care to drop the decoder monitor while
|
||||
// initializing.
|
||||
RefPtr<AudioStream> audioStream(new AudioStream());
|
||||
nsresult rv = audioStream->Init(mInfo.mChannels, mInfo.mRate,
|
||||
mChannel, AudioStream::HighLatency);
|
||||
nsresult rv = audioStream->Init(mInfo.mChannels, mInfo.mRate, mChannel);
|
||||
if (NS_FAILED(rv)) {
|
||||
audioStream->Shutdown();
|
||||
return rv;
|
||||
|
Loading…
x
Reference in New Issue
Block a user