2008-07-29 23:50:14 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 04:12:37 -07: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/. */
|
2012-11-14 11:46:40 -08:00
|
|
|
#if !defined(AudioStream_h_)
|
|
|
|
#define AudioStream_h_
|
2008-11-06 12:53:20 -08:00
|
|
|
|
2012-10-25 03:09:40 -07:00
|
|
|
#include "AudioSampleFormat.h"
|
2012-11-28 08:25:57 -08:00
|
|
|
#include "nsAutoPtr.h"
|
2013-09-24 19:10:24 -07:00
|
|
|
#include "nsCOMPtr.h"
|
2014-04-09 12:59:07 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2013-09-24 19:10:24 -07:00
|
|
|
#include "Latency.h"
|
2014-04-10 10:39:20 -07:00
|
|
|
#include "mozilla/dom/AudioChannelBinding.h"
|
2014-04-09 12:59:07 -07:00
|
|
|
#include "mozilla/RefPtr.h"
|
2014-08-18 10:12:08 -07:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2014-08-25 06:26:09 -07:00
|
|
|
#include "CubebUtils.h"
|
2013-11-27 21:09:08 -08:00
|
|
|
|
2013-09-05 13:25:17 -07:00
|
|
|
namespace soundtouch {
|
|
|
|
class SoundTouch;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
namespace mozilla {
|
|
|
|
|
2014-08-18 10:12:08 -07:00
|
|
|
template<>
|
|
|
|
struct DefaultDelete<cubeb_stream>
|
|
|
|
{
|
|
|
|
void operator()(cubeb_stream* aStream) const
|
|
|
|
{
|
|
|
|
cubeb_stream_destroy(aStream);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-22 02:38:28 -08:00
|
|
|
class AudioStream;
|
2014-06-25 19:56:23 -07:00
|
|
|
class FrameHistory;
|
2012-11-22 02:38:28 -08:00
|
|
|
|
|
|
|
class AudioClock
|
|
|
|
{
|
2013-11-27 21:09:08 -08:00
|
|
|
public:
|
2014-08-31 20:50:23 -07:00
|
|
|
explicit AudioClock(AudioStream* aStream);
|
2013-11-27 21:09:08 -08:00
|
|
|
// Initialize the clock with the current AudioStream. Need to be called
|
|
|
|
// before querying the clock. Called on the audio thread.
|
|
|
|
void Init();
|
|
|
|
// Update the number of samples that has been written in the audio backend.
|
|
|
|
// Called on the state machine thread.
|
2014-06-25 19:56:23 -07:00
|
|
|
void UpdateFrameHistory(uint32_t aServiced, uint32_t aUnderrun);
|
2013-11-27 21:09:08 -08:00
|
|
|
// Get the read position of the stream, in microseconds.
|
|
|
|
// Called on the state machine thead.
|
2014-05-21 00:28:22 -07:00
|
|
|
// Assumes the AudioStream lock is held and thus calls Unlocked versions
|
|
|
|
// of AudioStream funcs.
|
2014-06-25 19:56:23 -07:00
|
|
|
int64_t GetPositionUnlocked() const;
|
2013-11-27 21:09:08 -08:00
|
|
|
// Get the read position of the stream, in frames.
|
|
|
|
// Called on the state machine thead.
|
2014-06-25 19:56:23 -07:00
|
|
|
int64_t GetPositionInFrames() const;
|
2013-11-27 21:09:08 -08:00
|
|
|
// Set the playback rate.
|
|
|
|
// Called on the audio thread.
|
2014-05-21 00:28:22 -07:00
|
|
|
// Assumes the AudioStream lock is held and thus calls Unlocked versions
|
|
|
|
// of AudioStream funcs.
|
|
|
|
void SetPlaybackRateUnlocked(double aPlaybackRate);
|
2013-11-27 21:09:08 -08:00
|
|
|
// Get the current playback rate.
|
|
|
|
// Called on the audio thread.
|
2014-06-25 19:56:23 -07:00
|
|
|
double GetPlaybackRate() const;
|
2013-11-27 21:09:08 -08:00
|
|
|
// Set if we are preserving the pitch.
|
|
|
|
// Called on the audio thread.
|
|
|
|
void SetPreservesPitch(bool aPreservesPitch);
|
|
|
|
// Get the current pitch preservation state.
|
|
|
|
// Called on the audio thread.
|
2014-06-25 19:56:23 -07:00
|
|
|
bool GetPreservesPitch() const;
|
2013-11-27 21:09:08 -08:00
|
|
|
private:
|
|
|
|
// This AudioStream holds a strong reference to this AudioClock. This
|
|
|
|
// pointer is garanteed to always be valid.
|
2014-06-25 19:56:23 -07:00
|
|
|
AudioStream* const mAudioStream;
|
2013-11-27 21:09:08 -08:00
|
|
|
// Output rate in Hz (characteristic of the playback rate)
|
|
|
|
int mOutRate;
|
|
|
|
// Input rate in Hz (characteristic of the media being played)
|
|
|
|
int mInRate;
|
|
|
|
// True if the we are timestretching, false if we are resampling.
|
|
|
|
bool mPreservesPitch;
|
2014-06-25 19:56:23 -07:00
|
|
|
// The history of frames sent to the audio engine in each Datacallback.
|
|
|
|
const nsAutoPtr<FrameHistory> mFrameHistory;
|
2013-11-27 21:09:08 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
class CircularByteBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CircularByteBuffer()
|
|
|
|
: mBuffer(nullptr), mCapacity(0), mStart(0), mCount(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// Set the capacity of the buffer in bytes. Must be called before any
|
|
|
|
// call to append or pop elements.
|
|
|
|
void SetCapacity(uint32_t aCapacity) {
|
|
|
|
NS_ABORT_IF_FALSE(!mBuffer, "Buffer allocated.");
|
|
|
|
mCapacity = aCapacity;
|
|
|
|
mBuffer = new uint8_t[mCapacity];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Length() {
|
|
|
|
return mCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Capacity() {
|
|
|
|
return mCapacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Available() {
|
|
|
|
return Capacity() - Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append aLength bytes from aSrc to the buffer. Caller must check that
|
|
|
|
// sufficient space is available.
|
|
|
|
void AppendElements(const uint8_t* aSrc, uint32_t aLength) {
|
|
|
|
NS_ABORT_IF_FALSE(mBuffer && mCapacity, "Buffer not initialized.");
|
|
|
|
NS_ABORT_IF_FALSE(aLength <= Available(), "Buffer full.");
|
|
|
|
|
|
|
|
uint32_t end = (mStart + mCount) % mCapacity;
|
|
|
|
|
|
|
|
uint32_t toCopy = std::min(mCapacity - end, aLength);
|
|
|
|
memcpy(&mBuffer[end], aSrc, toCopy);
|
|
|
|
memcpy(&mBuffer[0], aSrc + toCopy, aLength - toCopy);
|
|
|
|
mCount += aLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove aSize bytes from the buffer. Caller must check returned size in
|
|
|
|
// aSize{1,2} before using the pointer returned in aData{1,2}. Caller
|
|
|
|
// must not specify an aSize larger than Length().
|
|
|
|
void PopElements(uint32_t aSize, void** aData1, uint32_t* aSize1,
|
|
|
|
void** aData2, uint32_t* aSize2) {
|
|
|
|
NS_ABORT_IF_FALSE(mBuffer && mCapacity, "Buffer not initialized.");
|
|
|
|
NS_ABORT_IF_FALSE(aSize <= Length(), "Request too large.");
|
|
|
|
|
|
|
|
*aData1 = &mBuffer[mStart];
|
|
|
|
*aSize1 = std::min(mCapacity - mStart, aSize);
|
|
|
|
*aData2 = &mBuffer[0];
|
|
|
|
*aSize2 = aSize - *aSize1;
|
|
|
|
mCount -= *aSize1 + *aSize2;
|
|
|
|
mStart += *aSize1 + *aSize2;
|
|
|
|
mStart %= mCapacity;
|
|
|
|
}
|
|
|
|
|
2014-04-09 12:59:07 -07:00
|
|
|
// Throw away all but aSize bytes from the buffer. Returns new size, which
|
|
|
|
// may be less than aSize
|
|
|
|
uint32_t ContractTo(uint32_t aSize) {
|
|
|
|
NS_ABORT_IF_FALSE(mBuffer && mCapacity, "Buffer not initialized.");
|
|
|
|
if (aSize >= mCount) {
|
|
|
|
return mCount;
|
|
|
|
}
|
|
|
|
mStart += (mCount - aSize);
|
|
|
|
mCount = aSize;
|
|
|
|
mStart %= mCapacity;
|
|
|
|
return mCount;
|
|
|
|
}
|
|
|
|
|
2014-04-13 11:08:10 -07:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = 0;
|
|
|
|
amount += mBuffer.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2014-07-18 10:22:03 -07:00
|
|
|
void Reset()
|
|
|
|
{
|
|
|
|
mBuffer = nullptr;
|
|
|
|
mCapacity = 0;
|
|
|
|
mStart = 0;
|
|
|
|
mCount = 0;
|
|
|
|
}
|
|
|
|
|
2013-11-27 21:09:08 -08:00
|
|
|
private:
|
|
|
|
nsAutoArrayPtr<uint8_t> mBuffer;
|
|
|
|
uint32_t mCapacity;
|
|
|
|
uint32_t mStart;
|
|
|
|
uint32_t mCount;
|
2012-11-22 02:38:28 -08:00
|
|
|
};
|
|
|
|
|
2014-04-09 12:59:07 -07:00
|
|
|
class AudioInitTask;
|
|
|
|
|
2012-05-31 21:45:01 -07:00
|
|
|
// 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
|
2014-07-29 09:45:03 -07:00
|
|
|
// GetPosition, GetPositionInFrames, SetVolume, and Get{Rate,Channels},
|
|
|
|
// SetMicrophoneActive is thread-safe without external synchronization.
|
2013-11-27 21:09:08 -08:00
|
|
|
class AudioStream MOZ_FINAL
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2014-06-20 04:08:24 -07:00
|
|
|
virtual ~AudioStream();
|
|
|
|
|
2010-11-16 20:14:19 -08:00
|
|
|
public:
|
2014-04-09 12:59:07 -07:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AudioStream)
|
2013-11-27 21:09:08 -08:00
|
|
|
AudioStream();
|
|
|
|
|
|
|
|
enum LatencyRequest {
|
|
|
|
HighLatency,
|
|
|
|
LowLatency
|
|
|
|
};
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// 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).
|
2013-11-27 21:09:08 -08:00
|
|
|
nsresult Init(int32_t aNumChannels, int32_t aRate,
|
2014-04-10 10:39:20 -07:00
|
|
|
const dom::AudioChannel aAudioStreamChannel,
|
2013-11-27 21:09:08 -08:00
|
|
|
LatencyRequest aLatencyRequest);
|
2008-07-29 23:50:14 -07:00
|
|
|
|
|
|
|
// Closes the stream. All future use of the stream is an error.
|
2013-11-27 21:09:08 -08:00
|
|
|
void Shutdown();
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2014-07-18 10:22:03 -07:00
|
|
|
void Reset();
|
|
|
|
|
2012-10-25 03:10:51 -07:00
|
|
|
// Write audio data to the audio hardware. aBuf is an array of AudioDataValues
|
|
|
|
// AudioDataValue of length aFrames*mChannels. If aFrames is larger
|
2011-09-26 20:31:18 -07:00
|
|
|
// than the result of Available(), the write will block until sufficient
|
2013-10-25 15:13:42 -07:00
|
|
|
// buffer space is available. aTime is the time in ms associated with the first sample
|
|
|
|
// for latency calculations
|
2013-11-27 21:09:08 -08:00
|
|
|
nsresult Write(const AudioDataValue* aBuf, uint32_t aFrames, TimeStamp* aTime = nullptr);
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// Return the number of audio frames that can be written without blocking.
|
2013-11-27 21:09:08 -08:00
|
|
|
uint32_t Available();
|
2008-10-19 00:39:21 -07:00
|
|
|
|
2008-07-29 23:50:14 -07:00
|
|
|
// Set the current volume of the audio playback. This is a value from
|
2012-05-31 21:45:01 -07:00
|
|
|
// 0 (meaning muted) to 1 (meaning full volume). Thread-safe.
|
2013-11-27 21:09:08 -08:00
|
|
|
void SetVolume(double aVolume);
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2014-07-29 09:45:03 -07:00
|
|
|
// Informs the AudioStream that a microphone is being used by someone in the
|
|
|
|
// application.
|
|
|
|
void SetMicrophoneActive(bool aActive);
|
|
|
|
void PanOutputIfNeeded(bool aMicrophoneActive);
|
2014-08-01 05:12:10 -07:00
|
|
|
void ResetStreamIfNeeded();
|
2014-07-29 09:45:03 -07:00
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
// Block until buffered audio data has been consumed.
|
2013-11-27 21:09:08 -08:00
|
|
|
void Drain();
|
2008-11-06 12:53:20 -08:00
|
|
|
|
2013-12-10 21:03:30 -08:00
|
|
|
// Break any blocking operation and set the stream to shutdown.
|
|
|
|
void Cancel();
|
|
|
|
|
2012-11-26 06:13:08 -08:00
|
|
|
// Start the stream.
|
2013-11-27 21:09:08 -08:00
|
|
|
void Start();
|
2012-11-26 06:13:08 -08:00
|
|
|
|
|
|
|
// Return the number of frames written so far in the stream. This allow the
|
|
|
|
// caller to check if it is safe to start the stream, if needed.
|
2013-11-27 21:09:08 -08:00
|
|
|
int64_t GetWritten();
|
2012-11-26 06:13:08 -08:00
|
|
|
|
|
|
|
// Pause audio playback.
|
2013-11-27 21:09:08 -08:00
|
|
|
void Pause();
|
2009-05-14 18:29:05 -07:00
|
|
|
|
2012-11-26 06:13:08 -08:00
|
|
|
// Resume audio playback.
|
2013-11-27 21:09:08 -08:00
|
|
|
void Resume();
|
2009-05-14 18:29:05 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// Return the position in microseconds of the audio frame being played by
|
2012-11-22 02:38:28 -08:00
|
|
|
// the audio hardware, compensated for playback rate change. Thread-safe.
|
2013-11-27 21:09:08 -08:00
|
|
|
int64_t GetPosition();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// Return the position, measured in audio frames played since the stream
|
2012-05-31 21:45:01 -07:00
|
|
|
// was opened, of the audio hardware. Thread-safe.
|
2013-11-27 21:09:08 -08:00
|
|
|
int64_t GetPositionInFrames();
|
2010-08-25 06:10:00 -07:00
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
// Returns true when the audio stream is paused.
|
2013-11-27 21:09:08 -08:00
|
|
|
bool IsPaused();
|
2010-11-29 21:37:32 -08:00
|
|
|
|
2012-11-22 02:38:28 -08:00
|
|
|
int GetRate() { return mOutRate; }
|
2012-02-13 20:39:33 -08:00
|
|
|
int GetChannels() { return mChannels; }
|
2013-09-26 12:06:59 -07:00
|
|
|
int GetOutChannels() { return mOutChannels; }
|
2012-10-25 03:09:38 -07:00
|
|
|
|
2012-11-22 02:38:28 -08:00
|
|
|
// Set playback rate as a multiple of the intrinsic playback rate. This is to
|
|
|
|
// be called only with aPlaybackRate > 0.0.
|
2013-11-27 21:09:08 -08:00
|
|
|
nsresult SetPlaybackRate(double aPlaybackRate);
|
2012-11-22 02:38:28 -08:00
|
|
|
// Switch between resampling (if false) and time stretching (if true, default).
|
2013-11-27 21:09:08 -08:00
|
|
|
nsresult SetPreservesPitch(bool aPreservesPitch);
|
|
|
|
|
2014-04-13 11:08:10 -07:00
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
2014-05-21 00:28:22 -07:00
|
|
|
protected:
|
|
|
|
friend class AudioClock;
|
|
|
|
|
2014-06-25 19:56:23 -07:00
|
|
|
// Return the position, measured in audio frames played since the stream was
|
|
|
|
// opened, of the audio hardware, not adjusted for the changes of playback
|
|
|
|
// rate or underrun frames.
|
2014-05-21 00:28:22 -07:00
|
|
|
// Caller must own the monitor.
|
|
|
|
int64_t GetPositionInFramesUnlocked();
|
|
|
|
|
2013-11-27 21:09:08 -08:00
|
|
|
private:
|
2014-04-09 12:59:07 -07:00
|
|
|
friend class AudioInitTask;
|
|
|
|
|
|
|
|
// So we can call it asynchronously from AudioInitTask
|
|
|
|
nsresult OpenCubeb(cubeb_stream_params &aParams,
|
|
|
|
LatencyRequest aLatencyRequest);
|
2014-08-13 17:08:00 -07:00
|
|
|
void AudioInitTaskFinished();
|
2014-04-09 12:59:07 -07:00
|
|
|
|
|
|
|
void CheckForStart();
|
|
|
|
|
2013-11-27 21:09:08 -08:00
|
|
|
static long DataCallback_S(cubeb_stream*, void* aThis, void* aBuffer, long aFrames)
|
|
|
|
{
|
|
|
|
return static_cast<AudioStream*>(aThis)->DataCallback(aBuffer, aFrames);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void StateCallback_S(cubeb_stream*, void* aThis, cubeb_state aState)
|
|
|
|
{
|
|
|
|
static_cast<AudioStream*>(aThis)->StateCallback(aState);
|
|
|
|
}
|
|
|
|
|
2014-07-29 09:45:03 -07:00
|
|
|
|
|
|
|
static void DeviceChangedCallback_s(void * aThis) {
|
|
|
|
static_cast<AudioStream*>(aThis)->DeviceChangedCallback();
|
|
|
|
}
|
|
|
|
|
2013-11-27 21:09:08 -08:00
|
|
|
long DataCallback(void* aBuffer, long aFrames);
|
|
|
|
void StateCallback(cubeb_state aState);
|
2014-07-29 09:45:03 -07:00
|
|
|
void DeviceChangedCallback();
|
2013-11-27 21:09:08 -08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
void StartUnlocked();
|
|
|
|
|
|
|
|
// The monitor is held to protect all access to member variables. Write()
|
|
|
|
// waits while mBuffer is full; DataCallback() notifies as it consumes
|
|
|
|
// data from mBuffer. Drain() waits while mState is DRAINING;
|
|
|
|
// StateCallback() notifies when mState is DRAINED.
|
|
|
|
Monitor mMonitor;
|
2012-11-22 02:38:28 -08:00
|
|
|
|
|
|
|
// Input rate in Hz (characteristic of the media being played)
|
|
|
|
int mInRate;
|
|
|
|
// Output rate in Hz (characteristic of the playback rate)
|
|
|
|
int mOutRate;
|
2012-02-13 20:39:33 -08:00
|
|
|
int mChannels;
|
2013-09-26 12:06:59 -07:00
|
|
|
int mOutChannels;
|
2014-07-18 10:22:03 -07:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
dom::AudioChannel mAudioChannel;
|
|
|
|
#endif
|
2012-11-26 06:13:08 -08:00
|
|
|
// Number of frames written to the buffers.
|
|
|
|
int64_t mWritten;
|
2012-11-22 02:38:28 -08:00
|
|
|
AudioClock mAudioClock;
|
2012-11-28 08:25:57 -08:00
|
|
|
nsAutoPtr<soundtouch::SoundTouch> mTimeStretcher;
|
2013-09-24 19:10:24 -07:00
|
|
|
nsRefPtr<AsyncLatencyLogger> mLatencyLog;
|
2013-10-25 15:13:42 -07:00
|
|
|
|
|
|
|
// copy of Latency logger's starting time for offset calculations
|
|
|
|
TimeStamp mStartTime;
|
2013-11-18 13:43:15 -08:00
|
|
|
// Whether we are playing a low latency stream, or a normal stream.
|
|
|
|
LatencyRequest mLatencyRequest;
|
2013-10-25 15:13:42 -07:00
|
|
|
// 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;
|
|
|
|
};
|
2013-11-27 21:09:08 -08:00
|
|
|
nsAutoTArray<Inserts, 8> mInserts;
|
|
|
|
|
|
|
|
// Output file for dumping audio
|
|
|
|
FILE* mDumpFile;
|
|
|
|
|
|
|
|
// Temporary audio buffer. Filled by Write() and consumed by
|
|
|
|
// DataCallback(). Once mBuffer is full, Write() blocks until sufficient
|
|
|
|
// space becomes available in mBuffer. mBuffer is sized in bytes, not
|
|
|
|
// frames.
|
|
|
|
CircularByteBuffer mBuffer;
|
|
|
|
|
2014-08-18 10:12:08 -07:00
|
|
|
// Owning reference to a cubeb_stream.
|
|
|
|
UniquePtr<cubeb_stream> mCubebStream;
|
2013-11-27 21:09:08 -08:00
|
|
|
|
|
|
|
uint32_t mBytesPerFrame;
|
|
|
|
|
|
|
|
uint32_t BytesToFrames(uint32_t aBytes) {
|
|
|
|
NS_ASSERTION(aBytes % mBytesPerFrame == 0,
|
|
|
|
"Byte count not aligned on frames size.");
|
|
|
|
return aBytes / mBytesPerFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t FramesToBytes(uint32_t aFrames) {
|
|
|
|
return aFrames * mBytesPerFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum StreamState {
|
|
|
|
INITIALIZED, // Initialized, playback has not begun.
|
2014-04-09 12:59:07 -07:00
|
|
|
STARTED, // cubeb started, but callbacks haven't started
|
|
|
|
RUNNING, // DataCallbacks have started after STARTED, or after Resume().
|
2013-11-27 21:09:08 -08:00
|
|
|
STOPPED, // Stopped by a call to Pause().
|
|
|
|
DRAINING, // Drain requested. DataCallback will indicate end of stream
|
|
|
|
// once the remaining contents of mBuffer are requested by
|
|
|
|
// cubeb, after which StateCallback will indicate drain
|
|
|
|
// completion.
|
|
|
|
DRAINED, // StateCallback has indicated that the drain is complete.
|
2014-04-09 12:59:07 -07:00
|
|
|
ERRORED, // Stream disabled due to an internal error.
|
|
|
|
SHUTDOWN // Shutdown has been called
|
2013-11-27 21:09:08 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
StreamState mState;
|
2014-04-09 12:59:07 -07:00
|
|
|
bool mNeedsStart; // needed in case Start() is called before cubeb is open
|
2014-06-04 11:52:32 -07:00
|
|
|
bool mIsFirst;
|
2014-07-29 09:45:03 -07:00
|
|
|
// True if a microphone is active.
|
|
|
|
bool mMicrophoneActive;
|
2014-07-18 10:20:00 -07:00
|
|
|
// 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;
|
2014-08-13 17:08:00 -07:00
|
|
|
// True if there is a pending AudioInitTask. Shutdown() will wait until the
|
|
|
|
// pending AudioInitTask is finished.
|
|
|
|
bool mPendingAudioInitTask;
|
2014-09-28 16:36:00 -07:00
|
|
|
// The last good position returned by cubeb_stream_get_position(). Used to
|
|
|
|
// check if the cubeb position is going backward.
|
|
|
|
uint64_t mLastGoodPosition;
|
2008-07-29 23:50:14 -07:00
|
|
|
};
|
2010-11-16 20:14:19 -08:00
|
|
|
|
2014-04-09 12:59:07 -07:00
|
|
|
class AudioInitTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AudioInitTask(AudioStream *aStream,
|
|
|
|
AudioStream::LatencyRequest aLatencyRequest,
|
|
|
|
const cubeb_stream_params &aParams)
|
|
|
|
: mAudioStream(aStream)
|
|
|
|
, mLatencyRequest(aLatencyRequest)
|
|
|
|
, mParams(aParams)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsresult Dispatch()
|
|
|
|
{
|
2014-04-17 12:13:44 -07:00
|
|
|
// 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)) {
|
2014-04-22 12:32:13 -07:00
|
|
|
// Note: event must not null out mThread!
|
2014-04-17 12:13:44 -07:00
|
|
|
rv = mThread->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
return rv;
|
2014-04-09 12:59:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~AudioInitTask() {};
|
|
|
|
|
|
|
|
private:
|
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE MOZ_FINAL;
|
|
|
|
|
|
|
|
RefPtr<AudioStream> mAudioStream;
|
|
|
|
AudioStream::LatencyRequest mLatencyRequest;
|
|
|
|
cubeb_stream_params mParams;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
};
|
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
#endif
|