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/. */
|
2008-07-30 06:50:14 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
2014-07-29 16:45:03 +00:00
|
|
|
#include <string.h>
|
2015-05-19 18:15:34 +00:00
|
|
|
#include "mozilla/Logging.h"
|
2011-05-18 21:12:25 +00:00
|
|
|
#include "prdtoa.h"
|
2012-11-14 19:46:40 +00:00
|
|
|
#include "AudioStream.h"
|
2011-04-13 22:12:23 +00:00
|
|
|
#include "VideoUtils.h"
|
2012-11-19 00:54:29 +00:00
|
|
|
#include "mozilla/Monitor.h"
|
2011-05-18 21:12:25 +00:00
|
|
|
#include "mozilla/Mutex.h"
|
2015-05-26 18:33:55 +00:00
|
|
|
#include "mozilla/Snprintf.h"
|
2013-01-15 12:22:03 +00:00
|
|
|
#include <algorithm>
|
2014-06-04 18:52:32 +00:00
|
|
|
#include "mozilla/Telemetry.h"
|
2014-08-25 13:26:09 +00:00
|
|
|
#include "CubebUtils.h"
|
2014-08-14 00:08:00 +00:00
|
|
|
#include "nsPrintfCString.h"
|
2015-08-06 06:30:22 +00:00
|
|
|
#include "gfxPrefs.h"
|
2016-04-08 07:37:31 +00:00
|
|
|
#include "AudioConverter.h"
|
2011-05-28 07:03:00 +00:00
|
|
|
|
2012-11-14 19:45:33 +00:00
|
|
|
namespace mozilla {
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2014-04-09 19:59:07 +00:00
|
|
|
#undef LOG
|
2016-01-20 02:28:36 +00:00
|
|
|
#undef LOGW
|
2014-04-09 19:59:07 +00:00
|
|
|
|
2015-11-15 13:49:01 +00:00
|
|
|
LazyLogModule gAudioStreamLog("AudioStream");
|
2014-04-09 19:59:07 +00:00
|
|
|
// For simple logs
|
2016-01-20 02:28:36 +00:00
|
|
|
#define LOG(x, ...) MOZ_LOG(gAudioStreamLog, mozilla::LogLevel::Debug, ("%p " x, this, ##__VA_ARGS__))
|
|
|
|
#define LOGW(x, ...) MOZ_LOG(gAudioStreamLog, mozilla::LogLevel::Warning, ("%p " x, this, ##__VA_ARGS__))
|
2008-07-30 06:50:14 +00:00
|
|
|
|
2013-04-03 23:12:27 +00:00
|
|
|
/**
|
|
|
|
* When MOZ_DUMP_AUDIO is set in the environment (to anything),
|
|
|
|
* we'll drop a series of files in the current working directory named
|
2013-11-28 05:09:08 +00:00
|
|
|
* dumped-audio-<nnn>.wav, one per AudioStream created, containing
|
2013-04-03 23:12:27 +00:00
|
|
|
* the audio for the stream including any skips due to underruns.
|
|
|
|
*/
|
|
|
|
static int gDumpedAudioCount = 0;
|
|
|
|
|
2014-06-26 02:56:23 +00:00
|
|
|
/**
|
|
|
|
* Keep a list of frames sent to the audio engine in each DataCallback along
|
|
|
|
* with the playback rate at the moment. Since the playback rate and number of
|
|
|
|
* underrun frames can vary in each callback. We need to keep the whole history
|
|
|
|
* in order to calculate the playback position of the audio engine correctly.
|
|
|
|
*/
|
|
|
|
class FrameHistory {
|
|
|
|
struct Chunk {
|
|
|
|
uint32_t servicedFrames;
|
|
|
|
uint32_t totalFrames;
|
2016-01-21 13:11:14 +00:00
|
|
|
uint32_t rate;
|
2014-06-26 02:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static T FramesToUs(uint32_t frames, int rate) {
|
|
|
|
return static_cast<T>(frames) * USECS_PER_S / rate;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
FrameHistory()
|
|
|
|
: mBaseOffset(0), mBasePosition(0) {}
|
|
|
|
|
2016-01-21 13:11:14 +00:00
|
|
|
void Append(uint32_t aServiced, uint32_t aUnderrun, uint32_t aRate) {
|
2014-06-26 02:56:23 +00:00
|
|
|
/* In most case where playback rate stays the same and we don't underrun
|
|
|
|
* frames, we are able to merge chunks to avoid lose of precision to add up
|
|
|
|
* in compressing chunks into |mBaseOffset| and |mBasePosition|.
|
|
|
|
*/
|
|
|
|
if (!mChunks.IsEmpty()) {
|
|
|
|
Chunk& c = mChunks.LastElement();
|
|
|
|
// 2 chunks (c1 and c2) can be merged when rate is the same and
|
|
|
|
// adjacent frames are zero. That is, underrun frames in c1 are zero
|
|
|
|
// or serviced frames in c2 are zero.
|
|
|
|
if (c.rate == aRate &&
|
|
|
|
(c.servicedFrames == c.totalFrames ||
|
|
|
|
aServiced == 0)) {
|
|
|
|
c.servicedFrames += aServiced;
|
|
|
|
c.totalFrames += aServiced + aUnderrun;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Chunk* p = mChunks.AppendElement();
|
|
|
|
p->servicedFrames = aServiced;
|
|
|
|
p->totalFrames = aServiced + aUnderrun;
|
|
|
|
p->rate = aRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param frames The playback position in frames of the audio engine.
|
|
|
|
* @return The playback position in microseconds of the audio engine,
|
|
|
|
* adjusted by playback rate changes and underrun frames.
|
|
|
|
*/
|
|
|
|
int64_t GetPosition(int64_t frames) {
|
|
|
|
// playback position should not go backward.
|
|
|
|
MOZ_ASSERT(frames >= mBaseOffset);
|
|
|
|
while (true) {
|
|
|
|
if (mChunks.IsEmpty()) {
|
|
|
|
return mBasePosition;
|
|
|
|
}
|
|
|
|
const Chunk& c = mChunks[0];
|
|
|
|
if (frames <= mBaseOffset + c.totalFrames) {
|
|
|
|
uint32_t delta = frames - mBaseOffset;
|
|
|
|
delta = std::min(delta, c.servicedFrames);
|
|
|
|
return static_cast<int64_t>(mBasePosition) +
|
|
|
|
FramesToUs<int64_t>(delta, c.rate);
|
|
|
|
}
|
|
|
|
// Since the playback position of the audio engine will not go backward,
|
|
|
|
// we are able to compress chunks so that |mChunks| won't grow unlimitedly.
|
|
|
|
// Note that we lose precision in converting integers into floats and
|
|
|
|
// inaccuracy will accumulate over time. However, for a 24hr long,
|
|
|
|
// sample rate = 44.1k file, the error will be less than 1 microsecond
|
|
|
|
// after playing 24 hours. So we are fine with that.
|
|
|
|
mBaseOffset += c.totalFrames;
|
|
|
|
mBasePosition += FramesToUs<double>(c.servicedFrames, c.rate);
|
|
|
|
mChunks.RemoveElementAt(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
2016-02-02 15:36:30 +00:00
|
|
|
AutoTArray<Chunk, 7> mChunks;
|
2014-06-26 02:56:23 +00:00
|
|
|
int64_t mBaseOffset;
|
|
|
|
double mBasePosition;
|
|
|
|
};
|
|
|
|
|
2016-01-12 13:48:25 +00:00
|
|
|
AudioStream::AudioStream(DataSource& aSource)
|
2013-11-28 05:09:08 +00:00
|
|
|
: mMonitor("AudioStream")
|
|
|
|
, mInRate(0)
|
|
|
|
, mOutRate(0)
|
|
|
|
, mChannels(0)
|
2013-09-26 19:06:59 +00:00
|
|
|
, mOutChannels(0)
|
2015-01-07 05:39:46 +00:00
|
|
|
, mAudioClock(this)
|
2015-07-06 19:08:56 +00:00
|
|
|
, mTimeStretcher(nullptr)
|
2013-11-28 05:09:08 +00:00
|
|
|
, mDumpFile(nullptr)
|
|
|
|
, mState(INITIALIZED)
|
2016-01-12 13:48:25 +00:00
|
|
|
, mDataSource(aSource)
|
2013-11-28 05:09:08 +00:00
|
|
|
{
|
|
|
|
}
|
2013-11-28 05:09:08 +00:00
|
|
|
|
2013-11-28 05:09:08 +00:00
|
|
|
AudioStream::~AudioStream()
|
|
|
|
{
|
2016-01-20 02:28:36 +00:00
|
|
|
LOG("deleted, state %d", mState);
|
2014-07-17 17:27:00 +00:00
|
|
|
MOZ_ASSERT(mState == SHUTDOWN && !mCubebStream,
|
|
|
|
"Should've called Shutdown() before deleting an AudioStream");
|
2013-11-28 05:09:08 +00:00
|
|
|
if (mDumpFile) {
|
|
|
|
fclose(mDumpFile);
|
|
|
|
}
|
2015-07-06 19:08:56 +00:00
|
|
|
if (mTimeStretcher) {
|
|
|
|
soundtouch::destroySoundTouchObj(mTimeStretcher);
|
|
|
|
}
|
2013-11-28 05:09:08 +00:00
|
|
|
}
|
|
|
|
|
2014-04-13 18:08:10 +00:00
|
|
|
size_t
|
|
|
|
AudioStream::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = aMallocSizeOf(this);
|
|
|
|
|
|
|
|
// Possibly add in the future:
|
|
|
|
// - mTimeStretcher
|
|
|
|
// - mCubebStream
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2013-11-28 05:09:08 +00:00
|
|
|
nsresult AudioStream::EnsureTimeStretcherInitializedUnlocked()
|
2012-11-22 10:38:28 +00:00
|
|
|
{
|
2013-11-28 05:09:08 +00:00
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2012-11-28 16:25:57 +00:00
|
|
|
if (!mTimeStretcher) {
|
2015-07-06 19:08:56 +00:00
|
|
|
mTimeStretcher = soundtouch::createSoundTouchObj();
|
2012-11-29 14:40:57 +00:00
|
|
|
mTimeStretcher->setSampleRate(mInRate);
|
2013-09-26 19:06:59 +00:00
|
|
|
mTimeStretcher->setChannels(mOutChannels);
|
2012-11-29 14:40:57 +00:00
|
|
|
mTimeStretcher->setPitch(1.0);
|
2012-11-22 10:38:28 +00:00
|
|
|
}
|
2013-03-04 14:48:58 +00:00
|
|
|
return NS_OK;
|
2012-11-22 10:38:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult AudioStream::SetPlaybackRate(double aPlaybackRate)
|
|
|
|
{
|
2014-07-17 17:27:00 +00:00
|
|
|
// MUST lock since the rate transposer is used from the cubeb callback,
|
|
|
|
// and rate changes can cause the buffer to be reallocated
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
|
2012-11-22 10:38:28 +00:00
|
|
|
NS_ASSERTION(aPlaybackRate > 0.0,
|
|
|
|
"Can't handle negative or null playbackrate in the AudioStream.");
|
|
|
|
// Avoid instantiating the resampler if we are not changing the playback rate.
|
2014-05-21 07:28:22 +00:00
|
|
|
// GetPreservesPitch/SetPreservesPitch don't need locking before calling
|
2012-11-22 10:38:28 +00:00
|
|
|
if (aPlaybackRate == mAudioClock.GetPlaybackRate()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-03-04 14:48:58 +00:00
|
|
|
|
2014-05-21 07:28:22 +00:00
|
|
|
if (EnsureTimeStretcherInitializedUnlocked() != NS_OK) {
|
2013-03-04 14:48:58 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-05-21 07:28:22 +00:00
|
|
|
mAudioClock.SetPlaybackRateUnlocked(aPlaybackRate);
|
2012-11-22 10:38:28 +00:00
|
|
|
mOutRate = mInRate / aPlaybackRate;
|
2012-11-29 14:40:57 +00:00
|
|
|
|
2012-11-22 10:38:28 +00:00
|
|
|
if (mAudioClock.GetPreservesPitch()) {
|
|
|
|
mTimeStretcher->setTempo(aPlaybackRate);
|
|
|
|
mTimeStretcher->setRate(1.0f);
|
|
|
|
} else {
|
|
|
|
mTimeStretcher->setTempo(1.0f);
|
|
|
|
mTimeStretcher->setRate(aPlaybackRate);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult AudioStream::SetPreservesPitch(bool aPreservesPitch)
|
|
|
|
{
|
2014-07-17 17:27:00 +00:00
|
|
|
// MUST lock since the rate transposer is used from the cubeb callback,
|
|
|
|
// and rate changes can cause the buffer to be reallocated
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
|
2012-11-22 10:38:28 +00:00
|
|
|
// Avoid instantiating the timestretcher instance if not needed.
|
|
|
|
if (aPreservesPitch == mAudioClock.GetPreservesPitch()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-29 14:40:57 +00:00
|
|
|
|
2014-05-21 07:28:22 +00:00
|
|
|
if (EnsureTimeStretcherInitializedUnlocked() != NS_OK) {
|
2013-03-04 14:48:58 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-11-29 14:40:57 +00:00
|
|
|
|
2012-11-22 10:38:28 +00:00
|
|
|
if (aPreservesPitch == true) {
|
|
|
|
mTimeStretcher->setTempo(mAudioClock.GetPlaybackRate());
|
|
|
|
mTimeStretcher->setRate(1.0f);
|
|
|
|
} else {
|
|
|
|
mTimeStretcher->setTempo(1.0f);
|
|
|
|
mTimeStretcher->setRate(mAudioClock.GetPlaybackRate());
|
|
|
|
}
|
|
|
|
|
|
|
|
mAudioClock.SetPreservesPitch(aPreservesPitch);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-13 01:26:59 +00:00
|
|
|
static void SetUint16LE(uint8_t* aDest, uint16_t aValue)
|
2013-04-03 23:12:27 +00:00
|
|
|
{
|
|
|
|
aDest[0] = aValue & 0xFF;
|
|
|
|
aDest[1] = aValue >> 8;
|
|
|
|
}
|
|
|
|
|
2013-06-13 01:26:59 +00:00
|
|
|
static void SetUint32LE(uint8_t* aDest, uint32_t aValue)
|
2013-04-03 23:12:27 +00:00
|
|
|
{
|
|
|
|
SetUint16LE(aDest, aValue & 0xFFFF);
|
|
|
|
SetUint16LE(aDest + 2, aValue >> 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
static FILE*
|
|
|
|
OpenDumpFile(AudioStream* aStream)
|
|
|
|
{
|
|
|
|
if (!getenv("MOZ_DUMP_AUDIO"))
|
|
|
|
return nullptr;
|
|
|
|
char buf[100];
|
2015-05-26 18:33:55 +00:00
|
|
|
snprintf_literal(buf, "dumped-audio-%d.wav", gDumpedAudioCount);
|
2013-04-03 23:12:27 +00:00
|
|
|
FILE* f = fopen(buf, "wb");
|
|
|
|
if (!f)
|
|
|
|
return nullptr;
|
|
|
|
++gDumpedAudioCount;
|
|
|
|
|
2013-06-13 01:26:59 +00:00
|
|
|
uint8_t header[] = {
|
2013-04-03 23:12:27 +00:00
|
|
|
// RIFF header
|
|
|
|
0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45,
|
|
|
|
// fmt chunk. We always write 16-bit samples.
|
|
|
|
0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0xFF, 0xFF,
|
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x10, 0x00,
|
|
|
|
// data chunk
|
|
|
|
0x64, 0x61, 0x74, 0x61, 0xFE, 0xFF, 0xFF, 0x7F
|
|
|
|
};
|
|
|
|
static const int CHANNEL_OFFSET = 22;
|
|
|
|
static const int SAMPLE_RATE_OFFSET = 24;
|
|
|
|
static const int BLOCK_ALIGN_OFFSET = 32;
|
|
|
|
SetUint16LE(header + CHANNEL_OFFSET, aStream->GetChannels());
|
|
|
|
SetUint32LE(header + SAMPLE_RATE_OFFSET, aStream->GetRate());
|
|
|
|
SetUint16LE(header + BLOCK_ALIGN_OFFSET, aStream->GetChannels()*2);
|
|
|
|
fwrite(header, sizeof(header), 1, f);
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2016-02-18 03:34:04 +00:00
|
|
|
template <typename T>
|
|
|
|
typename EnableIf<IsSame<T, int16_t>::value, void>::Type
|
|
|
|
WriteDumpFileHelper(T* aInput, size_t aSamples, FILE* aFile) {
|
|
|
|
fwrite(aInput, sizeof(T), aSamples, aFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename EnableIf<IsSame<T, float>::value, void>::Type
|
|
|
|
WriteDumpFileHelper(T* aInput, size_t aSamples, FILE* aFile) {
|
|
|
|
AutoTArray<uint8_t, 1024*2> buf;
|
|
|
|
buf.SetLength(aSamples*2);
|
|
|
|
uint8_t* output = buf.Elements();
|
|
|
|
for (uint32_t i = 0; i < aSamples; ++i) {
|
|
|
|
SetUint16LE(output + i*2, int16_t(aInput[i]*32767.0f));
|
|
|
|
}
|
|
|
|
fwrite(output, 2, aSamples, aFile);
|
|
|
|
fflush(aFile);
|
|
|
|
}
|
|
|
|
|
2013-04-03 23:12:27 +00:00
|
|
|
static void
|
2013-06-13 01:26:59 +00:00
|
|
|
WriteDumpFile(FILE* aDumpFile, AudioStream* aStream, uint32_t aFrames,
|
2013-04-03 23:12:27 +00:00
|
|
|
void* aBuffer)
|
|
|
|
{
|
|
|
|
if (!aDumpFile)
|
|
|
|
return;
|
|
|
|
|
2013-09-26 19:06:59 +00:00
|
|
|
uint32_t samples = aStream->GetOutChannels()*aFrames;
|
2013-04-03 23:12:27 +00:00
|
|
|
|
2016-02-18 03:34:04 +00:00
|
|
|
using SampleT = AudioSampleTraits<AUDIO_OUTPUT_FORMAT>::Type;
|
|
|
|
WriteDumpFileHelper(reinterpret_cast<SampleT*>(aBuffer), samples, aDumpFile);
|
2013-04-03 23:12:27 +00:00
|
|
|
}
|
|
|
|
|
2016-02-18 03:34:04 +00:00
|
|
|
template <AudioSampleFormat N>
|
|
|
|
struct ToCubebFormat {
|
|
|
|
static const cubeb_sample_format value = CUBEB_SAMPLE_FLOAT32NE;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ToCubebFormat<AUDIO_FORMAT_S16> {
|
|
|
|
static const cubeb_sample_format value = CUBEB_SAMPLE_S16NE;
|
|
|
|
};
|
|
|
|
|
2013-03-04 14:48:58 +00:00
|
|
|
nsresult
|
2016-01-21 13:11:14 +00:00
|
|
|
AudioStream::Init(uint32_t aNumChannels, uint32_t aRate,
|
2015-08-27 02:49:17 +00:00
|
|
|
const dom::AudioChannel aAudioChannel)
|
2012-01-12 21:20:36 +00:00
|
|
|
{
|
2014-06-04 18:52:32 +00:00
|
|
|
mStartTime = TimeStamp::Now();
|
2014-08-25 13:26:09 +00:00
|
|
|
mIsFirst = CubebUtils::GetFirstStream();
|
2014-06-04 18:52:32 +00:00
|
|
|
|
2016-01-21 13:11:14 +00:00
|
|
|
if (!CubebUtils::GetCubebContext()) {
|
2012-01-12 21:20:36 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-06-03 22:25:57 +00:00
|
|
|
MOZ_LOG(gAudioStreamLog, LogLevel::Debug,
|
2014-04-09 19:59:07 +00:00
|
|
|
("%s channels: %d, rate: %d for %p", __FUNCTION__, aNumChannels, aRate, this));
|
2012-11-22 10:38:28 +00:00
|
|
|
mInRate = mOutRate = aRate;
|
2012-01-12 21:20:36 +00:00
|
|
|
mChannels = aNumChannels;
|
2016-04-13 10:25:50 +00:00
|
|
|
mOutChannels = aNumChannels;
|
2012-01-12 21:20:36 +00:00
|
|
|
|
2013-04-03 23:12:27 +00:00
|
|
|
mDumpFile = OpenDumpFile(this);
|
|
|
|
|
2012-01-12 21:20:36 +00:00
|
|
|
cubeb_stream_params params;
|
|
|
|
params.rate = aRate;
|
2013-09-26 19:06:59 +00:00
|
|
|
params.channels = mOutChannels;
|
2013-03-12 03:46:32 +00:00
|
|
|
#if defined(__ANDROID__)
|
2013-03-21 02:36:29 +00:00
|
|
|
#if defined(MOZ_B2G)
|
2014-08-26 15:01:33 +00:00
|
|
|
params.stream_type = CubebUtils::ConvertChannelToCubebType(aAudioChannel);
|
2013-03-21 02:36:29 +00:00
|
|
|
#else
|
|
|
|
params.stream_type = CUBEB_STREAM_TYPE_MUSIC;
|
|
|
|
#endif
|
2013-03-12 03:46:32 +00:00
|
|
|
|
|
|
|
if (params.stream_type == CUBEB_STREAM_TYPE_MAX) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
#endif
|
2012-01-12 21:20:36 +00:00
|
|
|
|
2016-02-18 03:34:04 +00:00
|
|
|
params.format = ToCubebFormat<AUDIO_OUTPUT_FORMAT>::value;
|
2012-11-22 10:38:28 +00:00
|
|
|
mAudioClock.Init();
|
|
|
|
|
2015-08-27 02:49:17 +00:00
|
|
|
return OpenCubeb(params);
|
2014-07-29 16:45:03 +00:00
|
|
|
}
|
|
|
|
|
2014-04-09 19:59:07 +00:00
|
|
|
// 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
|
2015-08-27 02:49:17 +00:00
|
|
|
AudioStream::OpenCubeb(cubeb_stream_params &aParams)
|
2014-04-09 19:59:07 +00:00
|
|
|
{
|
2014-08-25 13:26:09 +00:00
|
|
|
cubeb* cubebContext = CubebUtils::GetCubebContext();
|
2014-04-09 19:59:07 +00:00
|
|
|
if (!cubebContext) {
|
2014-08-14 00:08:00 +00:00
|
|
|
NS_WARNING("Can't get cubeb context!");
|
2014-04-09 19:59:07 +00:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
mState = AudioStream::ERRORED;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-10-17 13:44:52 +00:00
|
|
|
// 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.
|
2015-08-27 02:49:17 +00:00
|
|
|
uint32_t latency = CubebUtils::GetCubebLatency();
|
2013-10-17 13:44:52 +00:00
|
|
|
|
2012-01-12 21:20:36 +00:00
|
|
|
{
|
|
|
|
cubeb_stream* stream;
|
2016-01-21 16:51:36 +00:00
|
|
|
if (cubeb_stream_init(cubebContext, &stream, "AudioStream",
|
|
|
|
nullptr, nullptr, nullptr, &aParams,
|
2013-10-17 13:44:52 +00:00
|
|
|
latency, DataCallback_S, StateCallback_S, this) == CUBEB_OK) {
|
2014-04-09 19:59:07 +00:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
2014-08-14 00:08:00 +00:00
|
|
|
MOZ_ASSERT(mState != SHUTDOWN);
|
2014-08-18 17:12:08 +00:00
|
|
|
mCubebStream.reset(stream);
|
2014-04-09 19:59:07 +00:00
|
|
|
} else {
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
mState = ERRORED;
|
2014-08-14 00:08:00 +00:00
|
|
|
NS_WARNING(nsPrintfCString("AudioStream::OpenCubeb() %p failed to init cubeb", this).get());
|
2014-04-09 19:59:07 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-11-18 21:43:15 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 17:22:03 +00:00
|
|
|
mState = INITIALIZED;
|
|
|
|
|
2014-06-04 18:52:32 +00:00
|
|
|
if (!mStartTime.IsNull()) {
|
2014-07-29 16:45:03 +00:00
|
|
|
TimeDuration timeDelta = TimeStamp::Now() - mStartTime;
|
2016-01-20 02:28:36 +00:00
|
|
|
LOG("creation time %sfirst: %u ms", mIsFirst ? "" : "not ",
|
|
|
|
(uint32_t) timeDelta.ToMilliseconds());
|
2014-07-29 16:45:03 +00:00
|
|
|
Telemetry::Accumulate(mIsFirst ? Telemetry::AUDIOSTREAM_FIRST_OPEN_MS :
|
|
|
|
Telemetry::AUDIOSTREAM_LATER_OPEN_MS, timeDelta.ToMilliseconds());
|
2014-06-04 18:52:32 +00:00
|
|
|
}
|
|
|
|
|
2012-01-12 21:20:36 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-28 05:09:08 +00:00
|
|
|
AudioStream::SetVolume(double aVolume)
|
2014-07-24 15:05:23 +00:00
|
|
|
{
|
2015-02-09 22:34:50 +00:00
|
|
|
MOZ_ASSERT(aVolume >= 0.0 && aVolume <= 1.0, "Invalid volume");
|
2014-07-29 16:45:02 +00:00
|
|
|
|
2014-08-25 12:13:14 +00:00
|
|
|
if (cubeb_stream_set_volume(mCubebStream.get(), aVolume * CubebUtils::GetVolumeScale()) != CUBEB_OK) {
|
2014-07-29 16:45:02 +00:00
|
|
|
NS_WARNING("Could not change volume on cubeb stream.");
|
|
|
|
}
|
2014-07-24 15:05:23 +00:00
|
|
|
}
|
|
|
|
|
2013-01-23 05:53:10 +00:00
|
|
|
void
|
2013-11-28 05:09:08 +00:00
|
|
|
AudioStream::Start()
|
2012-11-26 14:13:08 +00:00
|
|
|
{
|
2013-01-23 05:53:10 +00:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
StartUnlocked();
|
2012-11-26 14:13:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-23 05:53:10 +00:00
|
|
|
void
|
2013-11-28 05:09:08 +00:00
|
|
|
AudioStream::StartUnlocked()
|
2012-11-26 14:13:08 +00:00
|
|
|
{
|
2013-01-23 05:53:10 +00:00
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2014-04-09 19:59:07 +00:00
|
|
|
if (!mCubebStream) {
|
2013-01-23 05:53:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-07-29 16:45:05 +00:00
|
|
|
|
2014-04-09 19:59:07 +00:00
|
|
|
if (mState == INITIALIZED) {
|
2016-01-12 13:48:25 +00:00
|
|
|
mState = STARTED;
|
2014-07-17 17:27:00 +00:00
|
|
|
int r;
|
|
|
|
{
|
|
|
|
MonitorAutoUnlock mon(mMonitor);
|
2014-08-18 17:12:08 +00:00
|
|
|
r = cubeb_stream_start(mCubebStream.get());
|
2016-01-12 13:48:25 +00:00
|
|
|
// DataCallback might be called before we exit this scope
|
|
|
|
// if cubeb_stream_start() succeeds. mState must be set to STARTED
|
|
|
|
// beforehand.
|
|
|
|
}
|
|
|
|
if (r != CUBEB_OK) {
|
|
|
|
mState = ERRORED;
|
2014-07-17 17:27:00 +00:00
|
|
|
}
|
2016-01-20 02:28:36 +00:00
|
|
|
LOG("started, state %s", mState == STARTED ? "STARTED" : "ERRORED");
|
2013-01-23 05:53:10 +00:00
|
|
|
}
|
2012-11-26 14:13:08 +00:00
|
|
|
}
|
|
|
|
|
2012-01-12 21:20:36 +00:00
|
|
|
void
|
2013-11-28 05:09:08 +00:00
|
|
|
AudioStream::Pause()
|
2012-01-12 21:20:36 +00:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
2014-08-22 06:36:00 +00:00
|
|
|
|
|
|
|
if (mState == ERRORED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-09 19:59:07 +00:00
|
|
|
if (!mCubebStream || (mState != STARTED && mState != RUNNING)) {
|
|
|
|
mState = STOPPED; // which also tells async OpenCubeb not to start, just init
|
2012-01-12 21:20:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-01 04:45:01 +00:00
|
|
|
int r;
|
|
|
|
{
|
|
|
|
MonitorAutoUnlock mon(mMonitor);
|
2014-08-18 17:12:08 +00:00
|
|
|
r = cubeb_stream_stop(mCubebStream.get());
|
2012-06-01 04:45:01 +00:00
|
|
|
}
|
|
|
|
if (mState != ERRORED && r == CUBEB_OK) {
|
2012-01-12 21:20:36 +00:00
|
|
|
mState = STOPPED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-28 05:09:08 +00:00
|
|
|
AudioStream::Resume()
|
2012-01-12 21:20:36 +00:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
if (!mCubebStream || mState != STOPPED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-01 04:45:01 +00:00
|
|
|
int r;
|
|
|
|
{
|
|
|
|
MonitorAutoUnlock mon(mMonitor);
|
2014-08-18 17:12:08 +00:00
|
|
|
r = cubeb_stream_start(mCubebStream.get());
|
2012-06-01 04:45:01 +00:00
|
|
|
}
|
|
|
|
if (mState != ERRORED && r == CUBEB_OK) {
|
2012-01-12 21:20:36 +00:00
|
|
|
mState = STARTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 19:59:07 +00:00
|
|
|
void
|
|
|
|
AudioStream::Shutdown()
|
|
|
|
{
|
2014-07-17 17:27:00 +00:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
2016-01-20 02:28:36 +00:00
|
|
|
LOG("Shutdown, state %d", mState);
|
2014-07-17 17:27:00 +00:00
|
|
|
|
2014-04-09 19:59:07 +00:00
|
|
|
if (mCubebStream) {
|
2014-07-17 17:27:00 +00:00
|
|
|
MonitorAutoUnlock mon(mMonitor);
|
|
|
|
// Force stop to put the cubeb stream in a stable state before deletion.
|
2014-08-18 17:12:08 +00:00
|
|
|
cubeb_stream_stop(mCubebStream.get());
|
2014-07-17 17:27:00 +00:00
|
|
|
// Must not try to shut down cubeb from within the lock! wasapi may still
|
|
|
|
// call our callback after Pause()/stop()!?! Bug 996162
|
2014-04-09 19:59:07 +00:00
|
|
|
mCubebStream.reset();
|
|
|
|
}
|
2014-07-17 17:27:00 +00:00
|
|
|
|
|
|
|
mState = SHUTDOWN;
|
2014-04-09 19:59:07 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t
|
2013-11-28 05:09:08 +00:00
|
|
|
AudioStream::GetPosition()
|
2012-01-12 21:20:36 +00:00
|
|
|
{
|
2014-05-21 07:28:22 +00:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
return mAudioClock.GetPositionUnlocked();
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t
|
2013-11-28 05:09:08 +00:00
|
|
|
AudioStream::GetPositionInFrames()
|
2012-01-12 21:20:36 +00:00
|
|
|
{
|
2014-06-26 02:56:23 +00:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
2012-11-22 10:38:28 +00:00
|
|
|
return mAudioClock.GetPositionInFrames();
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t
|
2013-11-28 05:09:08 +00:00
|
|
|
AudioStream::GetPositionInFramesUnlocked()
|
2012-01-12 21:20:36 +00:00
|
|
|
{
|
|
|
|
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);
|
2014-08-18 17:12:08 +00:00
|
|
|
if (cubeb_stream_get_position(mCubebStream.get(), &position) != CUBEB_OK) {
|
2012-06-01 04:45:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 13:26:12 +00:00
|
|
|
return std::min<uint64_t>(position, INT64_MAX);
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-11-28 05:09:08 +00:00
|
|
|
AudioStream::IsPaused()
|
2012-01-12 21:20:36 +00:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
return mState == STOPPED;
|
|
|
|
}
|
|
|
|
|
2016-01-12 13:48:25 +00:00
|
|
|
bool
|
2016-04-13 10:25:50 +00:00
|
|
|
AudioStream::IsValidAudioFormat(Chunk* aChunk)
|
2016-01-12 13:48:25 +00:00
|
|
|
{
|
2016-01-21 13:11:14 +00:00
|
|
|
if (aChunk->Rate() != mInRate) {
|
|
|
|
LOGW("mismatched sample %u, mInRate=%u", aChunk->Rate(), mInRate);
|
2016-01-12 13:48:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-21 13:11:14 +00:00
|
|
|
if (aChunk->Channels() > 8) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-12 13:48:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-18 03:24:06 +00:00
|
|
|
void
|
|
|
|
AudioStream::GetUnprocessed(AudioBufferWriter& aWriter)
|
2012-11-22 10:38:28 +00:00
|
|
|
{
|
2014-07-17 17:27:00 +00:00
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2012-11-22 10:38:28 +00:00
|
|
|
|
|
|
|
// Flush the timestretcher pipeline, if we were playing using a playback rate
|
|
|
|
// other than 1.0.
|
|
|
|
if (mTimeStretcher && mTimeStretcher->numSamples()) {
|
2016-01-18 03:24:06 +00:00
|
|
|
auto timeStretcher = mTimeStretcher;
|
|
|
|
aWriter.Write([timeStretcher] (AudioDataValue* aPtr, uint32_t aFrames) {
|
|
|
|
return timeStretcher->receiveSamples(aPtr, aFrames);
|
|
|
|
}, aWriter.Available());
|
2016-01-12 13:48:25 +00:00
|
|
|
|
|
|
|
// TODO: There might be still unprocessed samples in the stretcher.
|
|
|
|
// We should either remove or flush them so they won't be in the output
|
|
|
|
// next time we switch a playback rate other than 1.0.
|
|
|
|
NS_WARN_IF(mTimeStretcher->numUnprocessedSamples() > 0);
|
2012-11-22 10:38:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 03:24:06 +00:00
|
|
|
while (aWriter.Available() > 0) {
|
|
|
|
UniquePtr<Chunk> c = mDataSource.PopFrames(aWriter.Available());
|
2016-01-12 13:48:25 +00:00
|
|
|
if (c->Frames() == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2016-01-18 03:24:06 +00:00
|
|
|
MOZ_ASSERT(c->Frames() <= aWriter.Available());
|
2016-04-13 10:25:50 +00:00
|
|
|
if (IsValidAudioFormat(c.get())) {
|
2016-01-18 03:24:06 +00:00
|
|
|
aWriter.Write(c->Data(), c->Frames());
|
2016-01-12 13:48:25 +00:00
|
|
|
} else {
|
2016-04-13 10:25:50 +00:00
|
|
|
// Write silence if invalid format.
|
2016-01-18 03:24:06 +00:00
|
|
|
aWriter.WriteZeros(c->Frames());
|
2016-01-12 13:48:25 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-22 10:38:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 03:24:06 +00:00
|
|
|
void
|
|
|
|
AudioStream::GetTimeStretched(AudioBufferWriter& aWriter)
|
2012-11-22 10:38:28 +00:00
|
|
|
{
|
2014-07-17 17:27:00 +00:00
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2012-11-29 14:40:57 +00:00
|
|
|
|
2013-01-18 13:21:47 +00:00
|
|
|
// We need to call the non-locking version, because we already have the lock.
|
2013-11-28 05:09:08 +00:00
|
|
|
if (EnsureTimeStretcherInitializedUnlocked() != NS_OK) {
|
2016-01-18 03:24:06 +00:00
|
|
|
return;
|
2013-03-04 14:48:58 +00:00
|
|
|
}
|
2012-11-29 14:40:57 +00:00
|
|
|
|
2012-11-22 10:38:28 +00:00
|
|
|
double playbackRate = static_cast<double>(mInRate) / mOutRate;
|
2016-01-18 03:24:06 +00:00
|
|
|
uint32_t toPopFrames = ceil(aWriter.Available() * playbackRate);
|
2016-01-12 13:48:25 +00:00
|
|
|
|
2016-01-18 03:24:06 +00:00
|
|
|
while (mTimeStretcher->numSamples() < aWriter.Available()) {
|
2016-01-12 13:48:25 +00:00
|
|
|
UniquePtr<Chunk> c = mDataSource.PopFrames(toPopFrames);
|
|
|
|
if (c->Frames() == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(c->Frames() <= toPopFrames);
|
2016-04-13 10:25:50 +00:00
|
|
|
if (IsValidAudioFormat(c.get())) {
|
2016-01-12 13:48:25 +00:00
|
|
|
mTimeStretcher->putSamples(c->Data(), c->Frames());
|
|
|
|
} else {
|
2016-04-13 10:25:50 +00:00
|
|
|
// Write silence if invalid format.
|
2016-02-02 15:36:30 +00:00
|
|
|
AutoTArray<AudioDataValue, 1000> buf;
|
2016-01-12 13:48:25 +00:00
|
|
|
buf.SetLength(mOutChannels * c->Frames());
|
|
|
|
memset(buf.Elements(), 0, buf.Length() * sizeof(AudioDataValue));
|
|
|
|
mTimeStretcher->putSamples(buf.Elements(), c->Frames());
|
2012-11-22 10:38:28 +00:00
|
|
|
}
|
2016-01-12 13:48:25 +00:00
|
|
|
}
|
2012-11-22 10:38:28 +00:00
|
|
|
|
2016-01-18 03:24:06 +00:00
|
|
|
auto timeStretcher = mTimeStretcher;
|
|
|
|
aWriter.Write([timeStretcher] (AudioDataValue* aPtr, uint32_t aFrames) {
|
|
|
|
return timeStretcher->receiveSamples(aPtr, aFrames);
|
|
|
|
}, aWriter.Available());
|
2012-11-22 10:38:28 +00:00
|
|
|
}
|
|
|
|
|
2012-01-12 21:20:36 +00:00
|
|
|
long
|
2013-11-28 05:09:08 +00:00
|
|
|
AudioStream::DataCallback(void* aBuffer, long aFrames)
|
2012-01-12 21:20:36 +00:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
2014-07-17 17:27:00 +00:00
|
|
|
MOZ_ASSERT(mState != SHUTDOWN, "No data callback after shutdown");
|
2016-01-18 03:24:06 +00:00
|
|
|
|
|
|
|
auto writer = AudioBufferWriter(
|
|
|
|
reinterpret_cast<AudioDataValue*>(aBuffer), mOutChannels, aFrames);
|
2014-07-18 17:20:00 +00:00
|
|
|
|
2016-01-12 13:48:25 +00:00
|
|
|
// FIXME: cubeb_pulse sometimes calls us before cubeb_stream_start() is called.
|
|
|
|
// We don't want to consume audio data until Start() is called by the client.
|
|
|
|
if (mState == INITIALIZED) {
|
|
|
|
NS_WARNING("data callback fires before cubeb_stream_start() is called");
|
|
|
|
mAudioClock.UpdateFrameHistory(0, aFrames);
|
2016-01-18 03:24:06 +00:00
|
|
|
return writer.WriteZeros(aFrames);
|
2016-01-12 13:48:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-09 19:59:07 +00:00
|
|
|
// NOTE: wasapi (others?) can call us back *after* stop()/Shutdown() (mState == SHUTDOWN)
|
|
|
|
// Bug 996162
|
|
|
|
|
|
|
|
// callback tells us cubeb succeeded initializing
|
|
|
|
if (mState == STARTED) {
|
|
|
|
mState = RUNNING;
|
|
|
|
}
|
|
|
|
|
2016-01-12 13:48:25 +00:00
|
|
|
if (mInRate == mOutRate) {
|
2016-01-18 03:24:06 +00:00
|
|
|
GetUnprocessed(writer);
|
2016-01-12 13:48:25 +00:00
|
|
|
} else {
|
2016-01-18 03:24:06 +00:00
|
|
|
GetTimeStretched(writer);
|
2012-05-03 04:48:54 +00:00
|
|
|
}
|
2012-01-12 21:20:36 +00:00
|
|
|
|
2014-06-26 02:56:23 +00:00
|
|
|
// Always send audible frames first, and silent frames later.
|
|
|
|
// Otherwise it will break the assumption of FrameHistory.
|
2016-01-12 13:48:25 +00:00
|
|
|
if (!mDataSource.Ended()) {
|
2016-01-18 03:24:06 +00:00
|
|
|
mAudioClock.UpdateFrameHistory(aFrames - writer.Available(), writer.Available());
|
|
|
|
if (writer.Available() > 0) {
|
2016-01-20 02:28:36 +00:00
|
|
|
LOGW("lost %d frames", writer.Available());
|
2016-01-18 03:24:06 +00:00
|
|
|
writer.WriteZeros(writer.Available());
|
2013-04-03 23:12:27 +00:00
|
|
|
}
|
2014-06-26 02:56:23 +00:00
|
|
|
} else {
|
2016-01-12 13:48:25 +00:00
|
|
|
// No more new data in the data source. Don't send silent frames so the
|
|
|
|
// cubeb stream can start draining.
|
2016-01-18 03:24:06 +00:00
|
|
|
mAudioClock.UpdateFrameHistory(aFrames - writer.Available(), 0);
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
|
|
|
|
2013-04-03 23:12:27 +00:00
|
|
|
WriteDumpFile(mDumpFile, this, aFrames, aBuffer);
|
|
|
|
|
2016-01-18 03:24:06 +00:00
|
|
|
return aFrames - writer.Available();
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
|
|
|
|
2012-07-16 21:15:24 +00:00
|
|
|
void
|
2013-11-28 05:09:08 +00:00
|
|
|
AudioStream::StateCallback(cubeb_state aState)
|
2012-01-12 21:20:36 +00:00
|
|
|
{
|
2012-06-01 04:45:01 +00:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
2014-07-17 17:27:00 +00:00
|
|
|
MOZ_ASSERT(mState != SHUTDOWN, "No state callback after shutdown");
|
2016-01-20 02:28:36 +00:00
|
|
|
LOG("StateCallback, mState=%d cubeb_state=%d", mState, aState);
|
2012-01-12 21:20:36 +00:00
|
|
|
if (aState == CUBEB_STATE_DRAINED) {
|
|
|
|
mState = DRAINED;
|
2016-01-12 13:48:25 +00:00
|
|
|
mDataSource.Drained();
|
2012-04-16 03:00:40 +00:00
|
|
|
} else if (aState == CUBEB_STATE_ERROR) {
|
2016-01-20 02:28:36 +00:00
|
|
|
LOG("StateCallback() state %d cubeb error", mState);
|
2012-04-16 03:00:40 +00:00
|
|
|
mState = ERRORED;
|
2012-01-12 21:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-22 10:38:28 +00:00
|
|
|
|
|
|
|
AudioClock::AudioClock(AudioStream* aStream)
|
|
|
|
:mAudioStream(aStream),
|
|
|
|
mOutRate(0),
|
|
|
|
mInRate(0),
|
|
|
|
mPreservesPitch(true),
|
2014-06-26 02:56:23 +00:00
|
|
|
mFrameHistory(new FrameHistory())
|
2012-11-22 10:38:28 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
void AudioClock::Init()
|
|
|
|
{
|
|
|
|
mOutRate = mAudioStream->GetRate();
|
|
|
|
mInRate = mAudioStream->GetRate();
|
|
|
|
}
|
|
|
|
|
2014-06-26 02:56:23 +00:00
|
|
|
void AudioClock::UpdateFrameHistory(uint32_t aServiced, uint32_t aUnderrun)
|
2012-11-22 10:38:28 +00:00
|
|
|
{
|
2014-06-26 02:56:23 +00:00
|
|
|
mFrameHistory->Append(aServiced, aUnderrun, mOutRate);
|
2012-11-22 10:38:28 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 02:56:23 +00:00
|
|
|
int64_t AudioClock::GetPositionUnlocked() const
|
2012-11-22 10:38:28 +00:00
|
|
|
{
|
2014-05-21 07:28:22 +00:00
|
|
|
// GetPositionInFramesUnlocked() asserts it owns the monitor
|
2014-06-26 02:56:23 +00:00
|
|
|
int64_t frames = mAudioStream->GetPositionInFramesUnlocked();
|
|
|
|
NS_ASSERTION(frames < 0 || (mInRate != 0 && mOutRate != 0), "AudioClock not initialized.");
|
|
|
|
return frames >= 0 ? mFrameHistory->GetPosition(frames) : -1;
|
2012-11-22 10:38:28 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 02:56:23 +00:00
|
|
|
int64_t AudioClock::GetPositionInFrames() const
|
2012-11-22 10:38:28 +00:00
|
|
|
{
|
2014-06-26 02:56:23 +00:00
|
|
|
return (GetPositionUnlocked() * mInRate) / USECS_PER_S;
|
2012-11-22 10:38:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 07:28:22 +00:00
|
|
|
void AudioClock::SetPlaybackRateUnlocked(double aPlaybackRate)
|
2012-11-22 10:38:28 +00:00
|
|
|
{
|
2016-01-21 13:11:14 +00:00
|
|
|
mOutRate = static_cast<uint32_t>(mInRate / aPlaybackRate);
|
2012-11-22 10:38:28 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 02:56:23 +00:00
|
|
|
double AudioClock::GetPlaybackRate() const
|
2012-11-22 10:38:28 +00:00
|
|
|
{
|
2012-12-28 22:01:38 +00:00
|
|
|
return static_cast<double>(mInRate) / mOutRate;
|
2012-11-22 10:38:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioClock::SetPreservesPitch(bool aPreservesPitch)
|
|
|
|
{
|
|
|
|
mPreservesPitch = aPreservesPitch;
|
|
|
|
}
|
|
|
|
|
2014-06-26 02:56:23 +00:00
|
|
|
bool AudioClock::GetPreservesPitch() const
|
2012-11-22 10:38:28 +00:00
|
|
|
{
|
|
|
|
return mPreservesPitch;
|
|
|
|
}
|
2015-07-13 15:25:42 +00:00
|
|
|
|
2012-11-14 19:45:33 +00:00
|
|
|
} // namespace mozilla
|