2008-07-30 10:50:14 +04: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 15:12:37 +04: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 10:50:14 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
2014-07-29 20:45:03 +04:00
|
|
|
#include <string.h>
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2011-05-19 01:12:25 +04:00
|
|
|
#include "prdtoa.h"
|
2012-11-14 23:46:40 +04:00
|
|
|
#include "AudioStream.h"
|
2011-04-14 02:12:23 +04:00
|
|
|
#include "VideoUtils.h"
|
2012-11-19 04:54:29 +04:00
|
|
|
#include "mozilla/Monitor.h"
|
2011-05-19 01:12:25 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2016-08-15 09:43:21 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
2017-10-09 23:01:48 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2014-06-04 22:52:32 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2014-08-25 17:26:09 +04:00
|
|
|
#include "CubebUtils.h"
|
2014-08-14 04:08:00 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2015-08-06 09:30:22 +03:00
|
|
|
#include "gfxPrefs.h"
|
2016-04-08 10:37:31 +03:00
|
|
|
#include "AudioConverter.h"
|
2017-09-14 06:56:35 +03:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#endif
|
2011-05-28 11:03:00 +04:00
|
|
|
|
2012-11-14 23:45:33 +04:00
|
|
|
namespace mozilla {
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2014-04-09 23:59:07 +04:00
|
|
|
#undef LOG
|
2016-01-20 05:28:36 +03:00
|
|
|
#undef LOGW
|
2017-10-09 05:18:37 +03:00
|
|
|
#undef LOGE
|
2014-04-09 23:59:07 +04:00
|
|
|
|
2015-11-15 16:49:01 +03:00
|
|
|
LazyLogModule gAudioStreamLog("AudioStream");
|
2014-04-09 23:59:07 +04:00
|
|
|
// For simple logs
|
2016-01-20 05:28:36 +03: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__))
|
2017-10-09 05:18:37 +03:00
|
|
|
#define LOGE(x, ...) NS_DebugBreak(NS_DEBUG_WARNING, nsPrintfCString("%p " x, this, ##__VA_ARGS__).get(), nullptr, __FILE__, __LINE__)
|
2008-07-30 10:50:14 +04:00
|
|
|
|
2014-06-26 06:56:23 +04: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 16:11:14 +03:00
|
|
|
uint32_t rate;
|
2014-06-26 06:56:23 +04: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 16:11:14 +03:00
|
|
|
void Append(uint32_t aServiced, uint32_t aUnderrun, uint32_t aRate) {
|
2014-06-26 06:56:23 +04: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 18:36:30 +03:00
|
|
|
AutoTArray<Chunk, 7> mChunks;
|
2014-06-26 06:56:23 +04:00
|
|
|
int64_t mBaseOffset;
|
|
|
|
double mBasePosition;
|
|
|
|
};
|
|
|
|
|
2016-01-12 16:48:25 +03:00
|
|
|
AudioStream::AudioStream(DataSource& aSource)
|
2013-11-28 09:09:08 +04:00
|
|
|
: mMonitor("AudioStream")
|
|
|
|
, mChannels(0)
|
2013-09-26 23:06:59 +04:00
|
|
|
, mOutChannels(0)
|
2015-07-06 22:08:56 +03:00
|
|
|
, mTimeStretcher(nullptr)
|
2013-11-28 09:09:08 +04:00
|
|
|
, mDumpFile(nullptr)
|
|
|
|
, mState(INITIALIZED)
|
2016-01-12 16:48:25 +03:00
|
|
|
, mDataSource(aSource)
|
2017-09-29 07:19:47 +03:00
|
|
|
, mPrefillQuirk(false)
|
2013-11-28 09:09:08 +04:00
|
|
|
{
|
2017-07-26 09:50:48 +03:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
if (XRE_IsContentProcess()) {
|
|
|
|
audio::AudioNotificationReceiver::Register(this);
|
|
|
|
}
|
|
|
|
#endif
|
2013-11-28 09:09:08 +04:00
|
|
|
}
|
2013-11-28 09:09:08 +04:00
|
|
|
|
2013-11-28 09:09:08 +04:00
|
|
|
AudioStream::~AudioStream()
|
|
|
|
{
|
2016-01-20 05:28:36 +03:00
|
|
|
LOG("deleted, state %d", mState);
|
2014-07-17 21:27:00 +04:00
|
|
|
MOZ_ASSERT(mState == SHUTDOWN && !mCubebStream,
|
|
|
|
"Should've called Shutdown() before deleting an AudioStream");
|
2013-11-28 09:09:08 +04:00
|
|
|
if (mDumpFile) {
|
|
|
|
fclose(mDumpFile);
|
|
|
|
}
|
2015-07-06 22:08:56 +03:00
|
|
|
if (mTimeStretcher) {
|
|
|
|
soundtouch::destroySoundTouchObj(mTimeStretcher);
|
|
|
|
}
|
2017-07-26 09:50:48 +03:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
if (XRE_IsContentProcess()) {
|
|
|
|
audio::AudioNotificationReceiver::Unregister(this);
|
|
|
|
}
|
|
|
|
#endif
|
2013-11-28 09:09:08 +04:00
|
|
|
}
|
|
|
|
|
2014-04-13 22:08:10 +04: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 09:09:08 +04:00
|
|
|
nsresult AudioStream::EnsureTimeStretcherInitializedUnlocked()
|
2012-11-22 14:38:28 +04:00
|
|
|
{
|
2013-11-28 09:09:08 +04:00
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2012-11-28 20:25:57 +04:00
|
|
|
if (!mTimeStretcher) {
|
2015-07-06 22:08:56 +03:00
|
|
|
mTimeStretcher = soundtouch::createSoundTouchObj();
|
2016-06-01 12:38:47 +03:00
|
|
|
mTimeStretcher->setSampleRate(mAudioClock.GetInputRate());
|
2013-09-26 23:06:59 +04:00
|
|
|
mTimeStretcher->setChannels(mOutChannels);
|
2012-11-29 18:40:57 +04:00
|
|
|
mTimeStretcher->setPitch(1.0);
|
2012-11-22 14:38:28 +04:00
|
|
|
}
|
2013-03-04 18:48:58 +04:00
|
|
|
return NS_OK;
|
2012-11-22 14:38:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult AudioStream::SetPlaybackRate(double aPlaybackRate)
|
|
|
|
{
|
2014-07-17 21:27:00 +04: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 14:38:28 +04: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 11:28:22 +04:00
|
|
|
// GetPreservesPitch/SetPreservesPitch don't need locking before calling
|
2012-11-22 14:38:28 +04:00
|
|
|
if (aPlaybackRate == mAudioClock.GetPlaybackRate()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-03-04 18:48:58 +04:00
|
|
|
|
2014-05-21 11:28:22 +04:00
|
|
|
if (EnsureTimeStretcherInitializedUnlocked() != NS_OK) {
|
2013-03-04 18:48:58 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-06-01 11:45:32 +03:00
|
|
|
mAudioClock.SetPlaybackRate(aPlaybackRate);
|
2012-11-29 18:40:57 +04:00
|
|
|
|
2012-11-22 14:38:28 +04: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 21:27:00 +04: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 14:38:28 +04:00
|
|
|
// Avoid instantiating the timestretcher instance if not needed.
|
|
|
|
if (aPreservesPitch == mAudioClock.GetPreservesPitch()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-29 18:40:57 +04:00
|
|
|
|
2014-05-21 11:28:22 +04:00
|
|
|
if (EnsureTimeStretcherInitializedUnlocked() != NS_OK) {
|
2013-03-04 18:48:58 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-11-29 18:40:57 +04:00
|
|
|
|
2012-11-22 14:38:28 +04: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 05:26:59 +04:00
|
|
|
static void SetUint16LE(uint8_t* aDest, uint16_t aValue)
|
2013-04-04 03:12:27 +04:00
|
|
|
{
|
|
|
|
aDest[0] = aValue & 0xFF;
|
|
|
|
aDest[1] = aValue >> 8;
|
|
|
|
}
|
|
|
|
|
2013-06-13 05:26:59 +04:00
|
|
|
static void SetUint32LE(uint8_t* aDest, uint32_t aValue)
|
2013-04-04 03:12:27 +04:00
|
|
|
{
|
|
|
|
SetUint16LE(aDest, aValue & 0xFFFF);
|
|
|
|
SetUint16LE(aDest + 2, aValue >> 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
static FILE*
|
2016-06-01 12:54:37 +03:00
|
|
|
OpenDumpFile(uint32_t aChannels, uint32_t aRate)
|
2013-04-04 03:12:27 +04:00
|
|
|
{
|
2016-06-02 10:53:39 +03: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
|
|
|
|
* dumped-audio-<nnn>.wav, one per AudioStream created, containing
|
|
|
|
* the audio for the stream including any skips due to underruns.
|
|
|
|
*/
|
|
|
|
static Atomic<int> gDumpedAudioCount(0);
|
|
|
|
|
2013-04-04 03:12:27 +04:00
|
|
|
if (!getenv("MOZ_DUMP_AUDIO"))
|
|
|
|
return nullptr;
|
|
|
|
char buf[100];
|
2016-08-15 09:44:00 +03:00
|
|
|
SprintfLiteral(buf, "dumped-audio-%d.wav", ++gDumpedAudioCount);
|
2013-04-04 03:12:27 +04:00
|
|
|
FILE* f = fopen(buf, "wb");
|
|
|
|
if (!f)
|
|
|
|
return nullptr;
|
|
|
|
|
2013-06-13 05:26:59 +04:00
|
|
|
uint8_t header[] = {
|
2013-04-04 03:12:27 +04: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;
|
2016-06-01 12:54:37 +03:00
|
|
|
SetUint16LE(header + CHANNEL_OFFSET, aChannels);
|
|
|
|
SetUint32LE(header + SAMPLE_RATE_OFFSET, aRate);
|
|
|
|
SetUint16LE(header + BLOCK_ALIGN_OFFSET, aChannels * 2);
|
2017-10-09 23:01:48 +03:00
|
|
|
Unused << fwrite(header, sizeof(header), 1, f);
|
2013-04-04 03:12:27 +04:00
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2016-02-18 06:34:04 +03:00
|
|
|
template <typename T>
|
|
|
|
typename EnableIf<IsSame<T, int16_t>::value, void>::Type
|
|
|
|
WriteDumpFileHelper(T* aInput, size_t aSamples, FILE* aFile) {
|
2017-10-09 23:01:48 +03:00
|
|
|
Unused << fwrite(aInput, sizeof(T), aSamples, aFile);
|
2016-02-18 06:34:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2017-10-09 23:01:48 +03:00
|
|
|
Unused << fwrite(output, 2, aSamples, aFile);
|
2016-02-18 06:34:04 +03:00
|
|
|
fflush(aFile);
|
|
|
|
}
|
|
|
|
|
2013-04-04 03:12:27 +04:00
|
|
|
static void
|
2013-06-13 05:26:59 +04:00
|
|
|
WriteDumpFile(FILE* aDumpFile, AudioStream* aStream, uint32_t aFrames,
|
2013-04-04 03:12:27 +04:00
|
|
|
void* aBuffer)
|
|
|
|
{
|
|
|
|
if (!aDumpFile)
|
|
|
|
return;
|
|
|
|
|
2013-09-26 23:06:59 +04:00
|
|
|
uint32_t samples = aStream->GetOutChannels()*aFrames;
|
2013-04-04 03:12:27 +04:00
|
|
|
|
2016-02-18 06:34:04 +03:00
|
|
|
using SampleT = AudioSampleTraits<AUDIO_OUTPUT_FORMAT>::Type;
|
|
|
|
WriteDumpFileHelper(reinterpret_cast<SampleT*>(aBuffer), samples, aDumpFile);
|
2013-04-04 03:12:27 +04:00
|
|
|
}
|
|
|
|
|
2016-02-18 06:34:04 +03: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;
|
|
|
|
};
|
|
|
|
|
2016-05-30 16:12:35 +03:00
|
|
|
template <typename Function, typename... Args>
|
|
|
|
int AudioStream::InvokeCubeb(Function aFunction, Args&&... aArgs)
|
|
|
|
{
|
|
|
|
MonitorAutoUnlock mon(mMonitor);
|
|
|
|
return aFunction(mCubebStream.get(), Forward<Args>(aArgs)...);
|
|
|
|
}
|
|
|
|
|
2013-03-04 18:48:58 +04:00
|
|
|
nsresult
|
2017-08-10 04:00:07 +03:00
|
|
|
AudioStream::Init(uint32_t aNumChannels, uint32_t aChannelMap, uint32_t aRate)
|
2012-01-13 01:20:36 +04:00
|
|
|
{
|
2016-05-30 11:38:37 +03:00
|
|
|
auto startTime = TimeStamp::Now();
|
2014-06-04 22:52:32 +04:00
|
|
|
|
2016-05-30 11:08:50 +03:00
|
|
|
LOG("%s channels: %d, rate: %d", __FUNCTION__, aNumChannels, aRate);
|
2012-01-13 01:20:36 +04:00
|
|
|
mChannels = aNumChannels;
|
2016-04-13 13:25:50 +03:00
|
|
|
mOutChannels = aNumChannels;
|
2012-01-13 01:20:36 +04:00
|
|
|
|
2016-06-01 12:54:37 +03:00
|
|
|
mDumpFile = OpenDumpFile(aNumChannels, aRate);
|
2013-04-04 03:12:27 +04:00
|
|
|
|
2012-01-13 01:20:36 +04:00
|
|
|
cubeb_stream_params params;
|
|
|
|
params.rate = aRate;
|
2013-09-26 23:06:59 +04:00
|
|
|
params.channels = mOutChannels;
|
2017-01-26 12:35:31 +03:00
|
|
|
params.layout = CubebUtils::ConvertChannelMapToCubebLayout(aChannelMap);
|
2016-02-18 06:34:04 +03:00
|
|
|
params.format = ToCubebFormat<AUDIO_OUTPUT_FORMAT>::value;
|
2018-01-22 21:20:29 +03:00
|
|
|
params.prefs = CUBEB_STREAM_PREF_NONE;
|
2017-08-10 04:00:07 +03:00
|
|
|
|
2016-06-01 11:36:10 +03:00
|
|
|
mAudioClock.Init(aRate);
|
2012-11-22 14:38:28 +04:00
|
|
|
|
2014-08-25 17:26:09 +04:00
|
|
|
cubeb* cubebContext = CubebUtils::GetCubebContext();
|
2014-04-09 23:59:07 +04:00
|
|
|
if (!cubebContext) {
|
2017-10-09 05:18:37 +03:00
|
|
|
LOGE("Can't get cubeb context!");
|
2016-09-16 03:54:24 +03:00
|
|
|
CubebUtils::ReportCubebStreamInitFailure(true);
|
2016-09-08 07:26:01 +03:00
|
|
|
return NS_ERROR_DOM_MEDIA_CUBEB_INITIALIZATION_ERR;
|
2014-04-09 23:59:07 +04:00
|
|
|
}
|
|
|
|
|
2017-09-29 07:19:47 +03:00
|
|
|
// cubeb's winmm backend prefills buffers on init rather than stream start.
|
|
|
|
// See https://github.com/kinetiknz/cubeb/issues/150
|
|
|
|
mPrefillQuirk = !strcmp(cubeb_get_backend_id(cubebContext), "winmm");
|
|
|
|
|
2016-09-16 03:54:24 +03:00
|
|
|
return OpenCubeb(cubebContext, params, startTime, CubebUtils::GetFirstStream());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AudioStream::OpenCubeb(cubeb* aContext, cubeb_stream_params& aParams,
|
|
|
|
TimeStamp aStartTime, bool aIsFirst)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aContext);
|
|
|
|
|
2016-05-31 08:55:27 +03:00
|
|
|
cubeb_stream* stream = nullptr;
|
2016-07-20 16:02:23 +03:00
|
|
|
/* Convert from milliseconds to frames. */
|
2016-09-09 16:21:50 +03:00
|
|
|
uint32_t latency_frames =
|
|
|
|
CubebUtils::GetCubebPlaybackLatencyInMilliseconds() * aParams.rate / 1000;
|
2016-09-16 03:54:24 +03:00
|
|
|
if (cubeb_stream_init(aContext, &stream, "AudioStream",
|
2016-05-31 08:55:27 +03:00
|
|
|
nullptr, nullptr, nullptr, &aParams,
|
2016-07-20 16:02:23 +03:00
|
|
|
latency_frames,
|
2016-05-31 08:55:27 +03:00
|
|
|
DataCallback_S, StateCallback_S, this) == CUBEB_OK) {
|
|
|
|
mCubebStream.reset(stream);
|
2016-06-23 18:50:52 +03:00
|
|
|
CubebUtils::ReportCubebBackendUsed();
|
2016-05-31 08:55:27 +03:00
|
|
|
} else {
|
2017-10-09 05:18:37 +03:00
|
|
|
LOGE("OpenCubeb() failed to init cubeb");
|
2016-06-23 18:50:52 +03:00
|
|
|
CubebUtils::ReportCubebStreamInitFailure(aIsFirst);
|
2016-05-31 08:55:27 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2013-11-19 01:43:15 +04:00
|
|
|
}
|
|
|
|
|
2016-05-30 11:38:37 +03:00
|
|
|
TimeDuration timeDelta = TimeStamp::Now() - aStartTime;
|
2016-05-31 09:18:58 +03:00
|
|
|
LOG("creation time %sfirst: %u ms", aIsFirst ? "" : "not ",
|
2016-05-30 11:38:37 +03:00
|
|
|
(uint32_t) timeDelta.ToMilliseconds());
|
2016-05-31 09:18:58 +03:00
|
|
|
Telemetry::Accumulate(aIsFirst ? Telemetry::AUDIOSTREAM_FIRST_OPEN_MS :
|
2016-05-30 11:38:37 +03:00
|
|
|
Telemetry::AUDIOSTREAM_LATER_OPEN_MS, timeDelta.ToMilliseconds());
|
2014-06-04 22:52:32 +04:00
|
|
|
|
2012-01-13 01:20:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-28 09:09:08 +04:00
|
|
|
AudioStream::SetVolume(double aVolume)
|
2014-07-24 19:05:23 +04:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aVolume >= 0.0 && aVolume <= 1.0, "Invalid volume");
|
2014-07-29 20:45:02 +04:00
|
|
|
|
2014-08-25 16:13:14 +04:00
|
|
|
if (cubeb_stream_set_volume(mCubebStream.get(), aVolume * CubebUtils::GetVolumeScale()) != CUBEB_OK) {
|
2017-10-09 05:18:37 +03:00
|
|
|
LOGE("Could not change volume on cubeb stream.");
|
2014-07-29 20:45:02 +04:00
|
|
|
}
|
2014-07-24 19:05:23 +04:00
|
|
|
}
|
|
|
|
|
2013-01-23 09:53:10 +04:00
|
|
|
void
|
2013-11-28 09:09:08 +04:00
|
|
|
AudioStream::Start()
|
2012-11-26 18:13:08 +04:00
|
|
|
{
|
2013-01-23 09:53:10 +04:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
2016-05-31 09:33:03 +03:00
|
|
|
MOZ_ASSERT(mState == INITIALIZED);
|
2016-07-12 17:53:58 +03:00
|
|
|
mState = STARTED;
|
2016-05-31 09:33:03 +03:00
|
|
|
auto r = InvokeCubeb(cubeb_stream_start);
|
2016-07-12 17:53:58 +03:00
|
|
|
if (r != CUBEB_OK) {
|
|
|
|
mState = ERRORED;
|
|
|
|
}
|
|
|
|
LOG("started, state %s", mState == STARTED ? "STARTED" : mState == DRAINED ? "DRAINED" : "ERRORED");
|
2012-11-26 18:13:08 +04:00
|
|
|
}
|
|
|
|
|
2012-01-13 01:20:36 +04:00
|
|
|
void
|
2013-11-28 09:09:08 +04:00
|
|
|
AudioStream::Pause()
|
2012-01-13 01:20:36 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
2016-05-31 10:00:55 +03:00
|
|
|
MOZ_ASSERT(mState != INITIALIZED, "Must be Start()ed.");
|
|
|
|
MOZ_ASSERT(mState != STOPPED, "Already Pause()ed.");
|
|
|
|
MOZ_ASSERT(mState != SHUTDOWN, "Already Shutdown()ed.");
|
2014-08-22 10:36:00 +04:00
|
|
|
|
2016-05-31 10:00:55 +03:00
|
|
|
// Do nothing if we are already drained or errored.
|
|
|
|
if (mState == DRAINED || mState == ERRORED) {
|
2014-08-22 10:36:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-31 10:00:55 +03:00
|
|
|
if (InvokeCubeb(cubeb_stream_stop) != CUBEB_OK) {
|
|
|
|
mState = ERRORED;
|
|
|
|
} else if (mState != DRAINED && mState != ERRORED) {
|
|
|
|
// Don't transition to other states if we are already
|
|
|
|
// drained or errored.
|
2012-01-13 01:20:36 +04:00
|
|
|
mState = STOPPED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-28 09:09:08 +04:00
|
|
|
AudioStream::Resume()
|
2012-01-13 01:20:36 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
2016-05-31 10:00:55 +03:00
|
|
|
MOZ_ASSERT(mState != INITIALIZED, "Must be Start()ed.");
|
|
|
|
MOZ_ASSERT(mState != STARTED, "Already Start()ed.");
|
|
|
|
MOZ_ASSERT(mState != SHUTDOWN, "Already Shutdown()ed.");
|
|
|
|
|
|
|
|
// Do nothing if we are already drained or errored.
|
|
|
|
if (mState == DRAINED || mState == ERRORED) {
|
2012-01-13 01:20:36 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-31 10:00:55 +03:00
|
|
|
if (InvokeCubeb(cubeb_stream_start) != CUBEB_OK) {
|
|
|
|
mState = ERRORED;
|
|
|
|
} else if (mState != DRAINED && mState != ERRORED) {
|
|
|
|
// Don't transition to other states if we are already
|
|
|
|
// drained or errored.
|
2012-01-13 01:20:36 +04:00
|
|
|
mState = STARTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 23:59:07 +04:00
|
|
|
void
|
|
|
|
AudioStream::Shutdown()
|
|
|
|
{
|
2014-07-17 21:27:00 +04:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
2016-01-20 05:28:36 +03:00
|
|
|
LOG("Shutdown, state %d", mState);
|
2014-07-17 21:27:00 +04:00
|
|
|
|
2014-04-09 23:59:07 +04:00
|
|
|
if (mCubebStream) {
|
2014-07-17 21:27:00 +04:00
|
|
|
MonitorAutoUnlock mon(mMonitor);
|
|
|
|
// Force stop to put the cubeb stream in a stable state before deletion.
|
2014-08-18 21:12:08 +04:00
|
|
|
cubeb_stream_stop(mCubebStream.get());
|
2014-07-17 21:27:00 +04: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 23:59:07 +04:00
|
|
|
mCubebStream.reset();
|
|
|
|
}
|
2014-07-17 21:27:00 +04:00
|
|
|
|
|
|
|
mState = SHUTDOWN;
|
2014-04-09 23:59:07 +04:00
|
|
|
}
|
|
|
|
|
2017-08-29 13:08:46 +03:00
|
|
|
#if defined(XP_WIN)
|
2017-07-12 09:51:07 +03:00
|
|
|
void
|
|
|
|
AudioStream::ResetDefaultDevice()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
2017-07-21 12:45:45 +03:00
|
|
|
if (mState != STARTED && mState != STOPPED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mCubebStream);
|
2017-07-12 09:51:07 +03:00
|
|
|
auto r = InvokeCubeb(cubeb_stream_reset_default_device);
|
|
|
|
if (!(r == CUBEB_OK || r == CUBEB_ERROR_NOT_SUPPORTED)) {
|
|
|
|
mState = ERRORED;
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 13:08:46 +03:00
|
|
|
#endif
|
2017-07-12 09:51:07 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t
|
2013-11-28 09:09:08 +04:00
|
|
|
AudioStream::GetPosition()
|
2012-01-13 01:20:36 +04:00
|
|
|
{
|
2014-05-21 11:28:22 +04:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
2016-06-01 11:19:35 +03:00
|
|
|
int64_t frames = GetPositionInFramesUnlocked();
|
|
|
|
return frames >= 0 ? mAudioClock.GetPosition(frames) : -1;
|
2012-01-13 01:20:36 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t
|
2013-11-28 09:09:08 +04:00
|
|
|
AudioStream::GetPositionInFrames()
|
2012-01-13 01:20:36 +04:00
|
|
|
{
|
2014-06-26 06:56:23 +04:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
2016-06-01 11:18:56 +03:00
|
|
|
int64_t frames = GetPositionInFramesUnlocked();
|
|
|
|
return frames >= 0 ? mAudioClock.GetPositionInFrames(frames) : -1;
|
2012-01-13 01:20:36 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t
|
2013-11-28 09:09:08 +04:00
|
|
|
AudioStream::GetPositionInFramesUnlocked()
|
2012-01-13 01:20:36 +04:00
|
|
|
{
|
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
|
|
|
|
2016-05-30 11:05:28 +03:00
|
|
|
if (mState == ERRORED) {
|
2012-01-13 01:20:36 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t position = 0;
|
2016-05-30 16:12:35 +03:00
|
|
|
if (InvokeCubeb(cubeb_stream_get_position, &position) != CUBEB_OK) {
|
|
|
|
return -1;
|
2012-01-13 01:20:36 +04:00
|
|
|
}
|
2015-11-02 16:26:12 +03:00
|
|
|
return std::min<uint64_t>(position, INT64_MAX);
|
2012-01-13 01:20:36 +04:00
|
|
|
}
|
|
|
|
|
2016-01-12 16:48:25 +03:00
|
|
|
bool
|
2016-04-13 13:25:50 +03:00
|
|
|
AudioStream::IsValidAudioFormat(Chunk* aChunk)
|
2016-01-12 16:48:25 +03:00
|
|
|
{
|
2016-06-01 12:38:47 +03:00
|
|
|
if (aChunk->Rate() != mAudioClock.GetInputRate()) {
|
|
|
|
LOGW("mismatched sample %u, mInRate=%u", aChunk->Rate(), mAudioClock.GetInputRate());
|
2016-01-12 16:48:25 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-21 16:11:14 +03:00
|
|
|
if (aChunk->Channels() > 8) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-12 16:48:25 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-18 06:24:06 +03:00
|
|
|
void
|
|
|
|
AudioStream::GetUnprocessed(AudioBufferWriter& aWriter)
|
2012-11-22 14:38:28 +04:00
|
|
|
{
|
2014-07-17 21:27:00 +04:00
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2012-11-22 14:38:28 +04:00
|
|
|
|
|
|
|
// Flush the timestretcher pipeline, if we were playing using a playback rate
|
|
|
|
// other than 1.0.
|
|
|
|
if (mTimeStretcher && mTimeStretcher->numSamples()) {
|
2016-01-18 06:24:06 +03:00
|
|
|
auto timeStretcher = mTimeStretcher;
|
|
|
|
aWriter.Write([timeStretcher] (AudioDataValue* aPtr, uint32_t aFrames) {
|
|
|
|
return timeStretcher->receiveSamples(aPtr, aFrames);
|
|
|
|
}, aWriter.Available());
|
2016-01-12 16:48:25 +03: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.
|
2016-09-02 10:12:24 +03:00
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
mTimeStretcher->numUnprocessedSamples() == 0, "no samples");
|
2012-11-22 14:38:28 +04:00
|
|
|
}
|
|
|
|
|
2016-01-18 06:24:06 +03:00
|
|
|
while (aWriter.Available() > 0) {
|
|
|
|
UniquePtr<Chunk> c = mDataSource.PopFrames(aWriter.Available());
|
2016-01-12 16:48:25 +03:00
|
|
|
if (c->Frames() == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2016-01-18 06:24:06 +03:00
|
|
|
MOZ_ASSERT(c->Frames() <= aWriter.Available());
|
2016-04-13 13:25:50 +03:00
|
|
|
if (IsValidAudioFormat(c.get())) {
|
2016-01-18 06:24:06 +03:00
|
|
|
aWriter.Write(c->Data(), c->Frames());
|
2016-01-12 16:48:25 +03:00
|
|
|
} else {
|
2016-04-13 13:25:50 +03:00
|
|
|
// Write silence if invalid format.
|
2016-01-18 06:24:06 +03:00
|
|
|
aWriter.WriteZeros(c->Frames());
|
2016-01-12 16:48:25 +03:00
|
|
|
}
|
|
|
|
}
|
2012-11-22 14:38:28 +04:00
|
|
|
}
|
|
|
|
|
2016-01-18 06:24:06 +03:00
|
|
|
void
|
|
|
|
AudioStream::GetTimeStretched(AudioBufferWriter& aWriter)
|
2012-11-22 14:38:28 +04:00
|
|
|
{
|
2014-07-17 21:27:00 +04:00
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2012-11-29 18:40:57 +04:00
|
|
|
|
2013-01-18 17:21:47 +04:00
|
|
|
// We need to call the non-locking version, because we already have the lock.
|
2013-11-28 09:09:08 +04:00
|
|
|
if (EnsureTimeStretcherInitializedUnlocked() != NS_OK) {
|
2016-01-18 06:24:06 +03:00
|
|
|
return;
|
2013-03-04 18:48:58 +04:00
|
|
|
}
|
2012-11-29 18:40:57 +04:00
|
|
|
|
2016-06-01 12:38:47 +03:00
|
|
|
uint32_t toPopFrames =
|
|
|
|
ceil(aWriter.Available() * mAudioClock.GetPlaybackRate());
|
2016-01-12 16:48:25 +03:00
|
|
|
|
2016-01-18 06:24:06 +03:00
|
|
|
while (mTimeStretcher->numSamples() < aWriter.Available()) {
|
2016-01-12 16:48:25 +03:00
|
|
|
UniquePtr<Chunk> c = mDataSource.PopFrames(toPopFrames);
|
|
|
|
if (c->Frames() == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(c->Frames() <= toPopFrames);
|
2016-04-13 13:25:50 +03:00
|
|
|
if (IsValidAudioFormat(c.get())) {
|
2016-01-12 16:48:25 +03:00
|
|
|
mTimeStretcher->putSamples(c->Data(), c->Frames());
|
|
|
|
} else {
|
2016-04-13 13:25:50 +03:00
|
|
|
// Write silence if invalid format.
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<AudioDataValue, 1000> buf;
|
2017-05-24 16:22:42 +03:00
|
|
|
auto size = CheckedUint32(mOutChannels) * c->Frames();
|
|
|
|
if (!size.isValid()) {
|
|
|
|
// The overflow should not happen in normal case.
|
|
|
|
LOGW("Invalid member data: %d channels, %d frames", mOutChannels, c->Frames());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buf.SetLength(size.value());
|
|
|
|
size = size * sizeof(AudioDataValue);
|
|
|
|
if (!size.isValid()) {
|
|
|
|
LOGW("The required memory size is too large.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memset(buf.Elements(), 0, size.value());
|
2016-01-12 16:48:25 +03:00
|
|
|
mTimeStretcher->putSamples(buf.Elements(), c->Frames());
|
2012-11-22 14:38:28 +04:00
|
|
|
}
|
2016-01-12 16:48:25 +03:00
|
|
|
}
|
2012-11-22 14:38:28 +04:00
|
|
|
|
2016-01-18 06:24:06 +03:00
|
|
|
auto timeStretcher = mTimeStretcher;
|
|
|
|
aWriter.Write([timeStretcher] (AudioDataValue* aPtr, uint32_t aFrames) {
|
|
|
|
return timeStretcher->receiveSamples(aPtr, aFrames);
|
|
|
|
}, aWriter.Available());
|
2012-11-22 14:38:28 +04:00
|
|
|
}
|
|
|
|
|
2012-01-13 01:20:36 +04:00
|
|
|
long
|
2013-11-28 09:09:08 +04:00
|
|
|
AudioStream::DataCallback(void* aBuffer, long aFrames)
|
2012-01-13 01:20:36 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
2014-07-17 21:27:00 +04:00
|
|
|
MOZ_ASSERT(mState != SHUTDOWN, "No data callback after shutdown");
|
2016-01-18 06:24:06 +03:00
|
|
|
|
|
|
|
auto writer = AudioBufferWriter(
|
|
|
|
reinterpret_cast<AudioDataValue*>(aBuffer), mOutChannels, aFrames);
|
2014-07-18 21:20:00 +04:00
|
|
|
|
2017-09-29 07:19:47 +03:00
|
|
|
if (mPrefillQuirk) {
|
2016-09-12 12:25:19 +03:00
|
|
|
// Don't consume audio data until Start() is called.
|
|
|
|
// Expected only with cubeb winmm backend.
|
|
|
|
if (mState == INITIALIZED) {
|
|
|
|
NS_WARNING("data callback fires before cubeb_stream_start() is called");
|
|
|
|
mAudioClock.UpdateFrameHistory(0, aFrames);
|
|
|
|
return writer.WriteZeros(aFrames);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mState != INITIALIZED);
|
|
|
|
}
|
2016-01-12 16:48:25 +03:00
|
|
|
|
2014-04-09 23:59:07 +04:00
|
|
|
// NOTE: wasapi (others?) can call us back *after* stop()/Shutdown() (mState == SHUTDOWN)
|
|
|
|
// Bug 996162
|
|
|
|
|
2016-06-01 12:38:47 +03:00
|
|
|
if (mAudioClock.GetInputRate() == mAudioClock.GetOutputRate()) {
|
2016-01-18 06:24:06 +03:00
|
|
|
GetUnprocessed(writer);
|
2016-01-12 16:48:25 +03:00
|
|
|
} else {
|
2016-01-18 06:24:06 +03:00
|
|
|
GetTimeStretched(writer);
|
2012-05-03 08:48:54 +04:00
|
|
|
}
|
2012-01-13 01:20:36 +04:00
|
|
|
|
2014-06-26 06:56:23 +04:00
|
|
|
// Always send audible frames first, and silent frames later.
|
|
|
|
// Otherwise it will break the assumption of FrameHistory.
|
2016-01-12 16:48:25 +03:00
|
|
|
if (!mDataSource.Ended()) {
|
2016-01-18 06:24:06 +03:00
|
|
|
mAudioClock.UpdateFrameHistory(aFrames - writer.Available(), writer.Available());
|
|
|
|
if (writer.Available() > 0) {
|
2016-01-20 05:28:36 +03:00
|
|
|
LOGW("lost %d frames", writer.Available());
|
2016-01-18 06:24:06 +03:00
|
|
|
writer.WriteZeros(writer.Available());
|
2013-04-04 03:12:27 +04:00
|
|
|
}
|
2014-06-26 06:56:23 +04:00
|
|
|
} else {
|
2016-01-12 16:48:25 +03:00
|
|
|
// No more new data in the data source. Don't send silent frames so the
|
|
|
|
// cubeb stream can start draining.
|
2016-01-18 06:24:06 +03:00
|
|
|
mAudioClock.UpdateFrameHistory(aFrames - writer.Available(), 0);
|
2012-01-13 01:20:36 +04:00
|
|
|
}
|
|
|
|
|
2013-04-04 03:12:27 +04:00
|
|
|
WriteDumpFile(mDumpFile, this, aFrames, aBuffer);
|
|
|
|
|
2016-01-18 06:24:06 +03:00
|
|
|
return aFrames - writer.Available();
|
2012-01-13 01:20:36 +04:00
|
|
|
}
|
|
|
|
|
2012-07-17 01:15:24 +04:00
|
|
|
void
|
2013-11-28 09:09:08 +04:00
|
|
|
AudioStream::StateCallback(cubeb_state aState)
|
2012-01-13 01:20:36 +04:00
|
|
|
{
|
2012-06-01 08:45:01 +04:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
2014-07-17 21:27:00 +04:00
|
|
|
MOZ_ASSERT(mState != SHUTDOWN, "No state callback after shutdown");
|
2016-01-20 05:28:36 +03:00
|
|
|
LOG("StateCallback, mState=%d cubeb_state=%d", mState, aState);
|
2012-01-13 01:20:36 +04:00
|
|
|
if (aState == CUBEB_STATE_DRAINED) {
|
|
|
|
mState = DRAINED;
|
2016-01-12 16:48:25 +03:00
|
|
|
mDataSource.Drained();
|
2012-04-16 07:00:40 +04:00
|
|
|
} else if (aState == CUBEB_STATE_ERROR) {
|
2017-10-09 05:18:37 +03:00
|
|
|
LOGE("StateCallback() state %d cubeb error", mState);
|
2012-04-16 07:00:40 +04:00
|
|
|
mState = ERRORED;
|
2012-01-13 01:20:36 +04:00
|
|
|
}
|
|
|
|
}
|
2012-11-22 14:38:28 +04:00
|
|
|
|
2016-06-01 11:36:10 +03:00
|
|
|
AudioClock::AudioClock()
|
|
|
|
: mOutRate(0),
|
2012-11-22 14:38:28 +04:00
|
|
|
mInRate(0),
|
|
|
|
mPreservesPitch(true),
|
2014-06-26 06:56:23 +04:00
|
|
|
mFrameHistory(new FrameHistory())
|
2012-11-22 14:38:28 +04:00
|
|
|
{}
|
|
|
|
|
2016-06-01 11:36:10 +03:00
|
|
|
void AudioClock::Init(uint32_t aRate)
|
2012-11-22 14:38:28 +04:00
|
|
|
{
|
2016-06-01 11:36:10 +03:00
|
|
|
mOutRate = aRate;
|
|
|
|
mInRate = aRate;
|
2012-11-22 14:38:28 +04:00
|
|
|
}
|
|
|
|
|
2014-06-26 06:56:23 +04:00
|
|
|
void AudioClock::UpdateFrameHistory(uint32_t aServiced, uint32_t aUnderrun)
|
2012-11-22 14:38:28 +04:00
|
|
|
{
|
2014-06-26 06:56:23 +04:00
|
|
|
mFrameHistory->Append(aServiced, aUnderrun, mOutRate);
|
2012-11-22 14:38:28 +04:00
|
|
|
}
|
|
|
|
|
2016-06-06 06:13:34 +03:00
|
|
|
int64_t AudioClock::GetPositionInFrames(int64_t aFrames) const
|
2012-11-22 14:38:28 +04:00
|
|
|
{
|
2016-06-06 06:13:34 +03:00
|
|
|
CheckedInt64 v = UsecsToFrames(GetPosition(aFrames), mInRate);
|
|
|
|
return v.isValid() ? v.value() : -1;
|
2012-11-22 14:38:28 +04:00
|
|
|
}
|
|
|
|
|
2016-06-01 11:19:35 +03:00
|
|
|
int64_t AudioClock::GetPosition(int64_t frames) const
|
|
|
|
{
|
|
|
|
return mFrameHistory->GetPosition(frames);
|
|
|
|
}
|
|
|
|
|
2016-06-01 11:45:32 +03:00
|
|
|
void AudioClock::SetPlaybackRate(double aPlaybackRate)
|
2012-11-22 14:38:28 +04:00
|
|
|
{
|
2016-01-21 16:11:14 +03:00
|
|
|
mOutRate = static_cast<uint32_t>(mInRate / aPlaybackRate);
|
2012-11-22 14:38:28 +04:00
|
|
|
}
|
|
|
|
|
2014-06-26 06:56:23 +04:00
|
|
|
double AudioClock::GetPlaybackRate() const
|
2012-11-22 14:38:28 +04:00
|
|
|
{
|
2012-12-29 02:01:38 +04:00
|
|
|
return static_cast<double>(mInRate) / mOutRate;
|
2012-11-22 14:38:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioClock::SetPreservesPitch(bool aPreservesPitch)
|
|
|
|
{
|
|
|
|
mPreservesPitch = aPreservesPitch;
|
|
|
|
}
|
|
|
|
|
2014-06-26 06:56:23 +04:00
|
|
|
bool AudioClock::GetPreservesPitch() const
|
2012-11-22 14:38:28 +04:00
|
|
|
{
|
|
|
|
return mPreservesPitch;
|
|
|
|
}
|
2015-07-13 18:25:42 +03:00
|
|
|
|
2012-11-14 23:45:33 +04:00
|
|
|
} // namespace mozilla
|