michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #include michael@0: #include michael@0: #include "prlog.h" michael@0: #include "prdtoa.h" michael@0: #include "AudioStream.h" michael@0: #include "VideoUtils.h" michael@0: #include "mozilla/Monitor.h" michael@0: #include "mozilla/Mutex.h" michael@0: #include michael@0: #include "mozilla/Preferences.h" michael@0: #include "soundtouch/SoundTouch.h" michael@0: #include "Latency.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: #ifdef LOG michael@0: #undef LOG michael@0: #endif michael@0: michael@0: #ifdef PR_LOGGING michael@0: PRLogModuleInfo* gAudioStreamLog = nullptr; michael@0: // For simple logs michael@0: #define LOG(x) PR_LOG(gAudioStreamLog, PR_LOG_DEBUG, x) michael@0: #else michael@0: #define LOG(x) michael@0: #endif michael@0: michael@0: /** michael@0: * When MOZ_DUMP_AUDIO is set in the environment (to anything), michael@0: * we'll drop a series of files in the current working directory named michael@0: * dumped-audio-.wav, one per AudioStream created, containing michael@0: * the audio for the stream including any skips due to underruns. michael@0: */ michael@0: static int gDumpedAudioCount = 0; michael@0: michael@0: #define PREF_VOLUME_SCALE "media.volume_scale" michael@0: #define PREF_CUBEB_LATENCY "media.cubeb_latency_ms" michael@0: michael@0: static const uint32_t CUBEB_NORMAL_LATENCY_MS = 100; michael@0: michael@0: StaticMutex AudioStream::sMutex; michael@0: cubeb* AudioStream::sCubebContext; michael@0: uint32_t AudioStream::sPreferredSampleRate; michael@0: double AudioStream::sVolumeScale; michael@0: uint32_t AudioStream::sCubebLatency; michael@0: bool AudioStream::sCubebLatencyPrefSet; michael@0: michael@0: /*static*/ void AudioStream::PrefChanged(const char* aPref, void* aClosure) michael@0: { michael@0: if (strcmp(aPref, PREF_VOLUME_SCALE) == 0) { michael@0: nsAdoptingString value = Preferences::GetString(aPref); michael@0: StaticMutexAutoLock lock(sMutex); michael@0: if (value.IsEmpty()) { michael@0: sVolumeScale = 1.0; michael@0: } else { michael@0: NS_ConvertUTF16toUTF8 utf8(value); michael@0: sVolumeScale = std::max(0, PR_strtod(utf8.get(), nullptr)); michael@0: } michael@0: } else if (strcmp(aPref, PREF_CUBEB_LATENCY) == 0) { michael@0: // Arbitrary default stream latency of 100ms. The higher this michael@0: // value, the longer stream volume changes will take to become michael@0: // audible. michael@0: sCubebLatencyPrefSet = Preferences::HasUserValue(aPref); michael@0: uint32_t value = Preferences::GetUint(aPref, CUBEB_NORMAL_LATENCY_MS); michael@0: StaticMutexAutoLock lock(sMutex); michael@0: sCubebLatency = std::min(std::max(value, 1), 1000); michael@0: } michael@0: } michael@0: michael@0: /*static*/ double AudioStream::GetVolumeScale() michael@0: { michael@0: StaticMutexAutoLock lock(sMutex); michael@0: return sVolumeScale; michael@0: } michael@0: michael@0: /*static*/ cubeb* AudioStream::GetCubebContext() michael@0: { michael@0: StaticMutexAutoLock lock(sMutex); michael@0: return GetCubebContextUnlocked(); michael@0: } michael@0: michael@0: /*static*/ void AudioStream::InitPreferredSampleRate() michael@0: { michael@0: StaticMutexAutoLock lock(sMutex); michael@0: if (sPreferredSampleRate == 0 && michael@0: cubeb_get_preferred_sample_rate(GetCubebContextUnlocked(), michael@0: &sPreferredSampleRate) != CUBEB_OK) { michael@0: sPreferredSampleRate = 44100; michael@0: } michael@0: } michael@0: michael@0: /*static*/ cubeb* AudioStream::GetCubebContextUnlocked() michael@0: { michael@0: sMutex.AssertCurrentThreadOwns(); michael@0: if (sCubebContext || michael@0: cubeb_init(&sCubebContext, "AudioStream") == CUBEB_OK) { michael@0: return sCubebContext; michael@0: } michael@0: NS_WARNING("cubeb_init failed"); michael@0: return nullptr; michael@0: } michael@0: michael@0: /*static*/ uint32_t AudioStream::GetCubebLatency() michael@0: { michael@0: StaticMutexAutoLock lock(sMutex); michael@0: return sCubebLatency; michael@0: } michael@0: michael@0: /*static*/ bool AudioStream::CubebLatencyPrefSet() michael@0: { michael@0: StaticMutexAutoLock lock(sMutex); michael@0: return sCubebLatencyPrefSet; michael@0: } michael@0: michael@0: #if defined(__ANDROID__) && defined(MOZ_B2G) michael@0: static cubeb_stream_type ConvertChannelToCubebType(dom::AudioChannel aChannel) michael@0: { michael@0: switch(aChannel) { michael@0: case dom::AudioChannel::Normal: michael@0: return CUBEB_STREAM_TYPE_SYSTEM; michael@0: case dom::AudioChannel::Content: michael@0: return CUBEB_STREAM_TYPE_MUSIC; michael@0: case dom::AudioChannel::Notification: michael@0: return CUBEB_STREAM_TYPE_NOTIFICATION; michael@0: case dom::AudioChannel::Alarm: michael@0: return CUBEB_STREAM_TYPE_ALARM; michael@0: case dom::AudioChannel::Telephony: michael@0: return CUBEB_STREAM_TYPE_VOICE_CALL; michael@0: case dom::AudioChannel::Ringer: michael@0: return CUBEB_STREAM_TYPE_RING; michael@0: // Currently Android openSLES library doesn't support FORCE_AUDIBLE yet. michael@0: case dom::AudioChannel::Publicnotification: michael@0: default: michael@0: NS_ERROR("The value of AudioChannel is invalid"); michael@0: return CUBEB_STREAM_TYPE_MAX; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: AudioStream::AudioStream() michael@0: : mMonitor("AudioStream") michael@0: , mInRate(0) michael@0: , mOutRate(0) michael@0: , mChannels(0) michael@0: , mOutChannels(0) michael@0: , mWritten(0) michael@0: , mAudioClock(MOZ_THIS_IN_INITIALIZER_LIST()) michael@0: , mLatencyRequest(HighLatency) michael@0: , mReadPoint(0) michael@0: , mLostFrames(0) michael@0: , mDumpFile(nullptr) michael@0: , mVolume(1.0) michael@0: , mBytesPerFrame(0) michael@0: , mState(INITIALIZED) michael@0: , mNeedsStart(false) michael@0: { michael@0: // keep a ref in case we shut down later than nsLayoutStatics michael@0: mLatencyLog = AsyncLatencyLogger::Get(true); michael@0: } michael@0: michael@0: AudioStream::~AudioStream() michael@0: { michael@0: LOG(("AudioStream: delete %p, state %d", this, mState)); michael@0: Shutdown(); michael@0: if (mDumpFile) { michael@0: fclose(mDumpFile); michael@0: } michael@0: } michael@0: michael@0: size_t michael@0: AudioStream::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: size_t amount = aMallocSizeOf(this); michael@0: michael@0: // Possibly add in the future: michael@0: // - mTimeStretcher michael@0: // - mLatencyLog michael@0: // - mCubebStream michael@0: michael@0: amount += mInserts.SizeOfExcludingThis(aMallocSizeOf); michael@0: amount += mBuffer.SizeOfExcludingThis(aMallocSizeOf); michael@0: michael@0: return amount; michael@0: } michael@0: michael@0: /*static*/ void AudioStream::InitLibrary() michael@0: { michael@0: #ifdef PR_LOGGING michael@0: gAudioStreamLog = PR_NewLogModule("AudioStream"); michael@0: #endif michael@0: PrefChanged(PREF_VOLUME_SCALE, nullptr); michael@0: Preferences::RegisterCallback(PrefChanged, PREF_VOLUME_SCALE); michael@0: PrefChanged(PREF_CUBEB_LATENCY, nullptr); michael@0: Preferences::RegisterCallback(PrefChanged, PREF_CUBEB_LATENCY); michael@0: } michael@0: michael@0: /*static*/ void AudioStream::ShutdownLibrary() michael@0: { michael@0: Preferences::UnregisterCallback(PrefChanged, PREF_VOLUME_SCALE); michael@0: Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_LATENCY); michael@0: michael@0: StaticMutexAutoLock lock(sMutex); michael@0: if (sCubebContext) { michael@0: cubeb_destroy(sCubebContext); michael@0: sCubebContext = nullptr; michael@0: } michael@0: } michael@0: michael@0: nsresult AudioStream::EnsureTimeStretcherInitializedUnlocked() michael@0: { michael@0: mMonitor.AssertCurrentThreadOwns(); michael@0: if (!mTimeStretcher) { michael@0: mTimeStretcher = new soundtouch::SoundTouch(); michael@0: mTimeStretcher->setSampleRate(mInRate); michael@0: mTimeStretcher->setChannels(mOutChannels); michael@0: mTimeStretcher->setPitch(1.0); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult AudioStream::SetPlaybackRate(double aPlaybackRate) michael@0: { michael@0: NS_ASSERTION(aPlaybackRate > 0.0, michael@0: "Can't handle negative or null playbackrate in the AudioStream."); michael@0: // Avoid instantiating the resampler if we are not changing the playback rate. michael@0: // GetPreservesPitch/SetPreservesPitch don't need locking before calling michael@0: if (aPlaybackRate == mAudioClock.GetPlaybackRate()) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: // MUST lock since the rate transposer is used from the cubeb callback, michael@0: // and rate changes can cause the buffer to be reallocated michael@0: MonitorAutoLock mon(mMonitor); michael@0: if (EnsureTimeStretcherInitializedUnlocked() != NS_OK) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: mAudioClock.SetPlaybackRateUnlocked(aPlaybackRate); michael@0: mOutRate = mInRate / aPlaybackRate; michael@0: michael@0: if (mAudioClock.GetPreservesPitch()) { michael@0: mTimeStretcher->setTempo(aPlaybackRate); michael@0: mTimeStretcher->setRate(1.0f); michael@0: } else { michael@0: mTimeStretcher->setTempo(1.0f); michael@0: mTimeStretcher->setRate(aPlaybackRate); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult AudioStream::SetPreservesPitch(bool aPreservesPitch) michael@0: { michael@0: // Avoid instantiating the timestretcher instance if not needed. michael@0: if (aPreservesPitch == mAudioClock.GetPreservesPitch()) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: // MUST lock since the rate transposer is used from the cubeb callback, michael@0: // and rate changes can cause the buffer to be reallocated michael@0: MonitorAutoLock mon(mMonitor); michael@0: if (EnsureTimeStretcherInitializedUnlocked() != NS_OK) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: if (aPreservesPitch == true) { michael@0: mTimeStretcher->setTempo(mAudioClock.GetPlaybackRate()); michael@0: mTimeStretcher->setRate(1.0f); michael@0: } else { michael@0: mTimeStretcher->setTempo(1.0f); michael@0: mTimeStretcher->setRate(mAudioClock.GetPlaybackRate()); michael@0: } michael@0: michael@0: mAudioClock.SetPreservesPitch(aPreservesPitch); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: int64_t AudioStream::GetWritten() michael@0: { michael@0: return mWritten; michael@0: } michael@0: michael@0: /*static*/ int AudioStream::MaxNumberOfChannels() michael@0: { michael@0: cubeb* cubebContext = GetCubebContext(); michael@0: uint32_t maxNumberOfChannels; michael@0: if (cubebContext && michael@0: cubeb_get_max_channel_count(cubebContext, michael@0: &maxNumberOfChannels) == CUBEB_OK) { michael@0: return static_cast(maxNumberOfChannels); michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: /*static*/ int AudioStream::PreferredSampleRate() michael@0: { michael@0: MOZ_ASSERT(sPreferredSampleRate, michael@0: "sPreferredSampleRate has not been initialized!"); michael@0: return sPreferredSampleRate; michael@0: } michael@0: michael@0: static void SetUint16LE(uint8_t* aDest, uint16_t aValue) michael@0: { michael@0: aDest[0] = aValue & 0xFF; michael@0: aDest[1] = aValue >> 8; michael@0: } michael@0: michael@0: static void SetUint32LE(uint8_t* aDest, uint32_t aValue) michael@0: { michael@0: SetUint16LE(aDest, aValue & 0xFFFF); michael@0: SetUint16LE(aDest + 2, aValue >> 16); michael@0: } michael@0: michael@0: static FILE* michael@0: OpenDumpFile(AudioStream* aStream) michael@0: { michael@0: if (!getenv("MOZ_DUMP_AUDIO")) michael@0: return nullptr; michael@0: char buf[100]; michael@0: sprintf(buf, "dumped-audio-%d.wav", gDumpedAudioCount); michael@0: FILE* f = fopen(buf, "wb"); michael@0: if (!f) michael@0: return nullptr; michael@0: ++gDumpedAudioCount; michael@0: michael@0: uint8_t header[] = { michael@0: // RIFF header michael@0: 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45, michael@0: // fmt chunk. We always write 16-bit samples. michael@0: 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0xFF, 0xFF, michael@0: 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x10, 0x00, michael@0: // data chunk michael@0: 0x64, 0x61, 0x74, 0x61, 0xFE, 0xFF, 0xFF, 0x7F michael@0: }; michael@0: static const int CHANNEL_OFFSET = 22; michael@0: static const int SAMPLE_RATE_OFFSET = 24; michael@0: static const int BLOCK_ALIGN_OFFSET = 32; michael@0: SetUint16LE(header + CHANNEL_OFFSET, aStream->GetChannels()); michael@0: SetUint32LE(header + SAMPLE_RATE_OFFSET, aStream->GetRate()); michael@0: SetUint16LE(header + BLOCK_ALIGN_OFFSET, aStream->GetChannels()*2); michael@0: fwrite(header, sizeof(header), 1, f); michael@0: michael@0: return f; michael@0: } michael@0: michael@0: static void michael@0: WriteDumpFile(FILE* aDumpFile, AudioStream* aStream, uint32_t aFrames, michael@0: void* aBuffer) michael@0: { michael@0: if (!aDumpFile) michael@0: return; michael@0: michael@0: uint32_t samples = aStream->GetOutChannels()*aFrames; michael@0: if (AUDIO_OUTPUT_FORMAT == AUDIO_FORMAT_S16) { michael@0: fwrite(aBuffer, 2, samples, aDumpFile); michael@0: return; michael@0: } michael@0: michael@0: NS_ASSERTION(AUDIO_OUTPUT_FORMAT == AUDIO_FORMAT_FLOAT32, "bad format"); michael@0: nsAutoTArray buf; michael@0: buf.SetLength(samples*2); michael@0: float* input = static_cast(aBuffer); michael@0: uint8_t* output = buf.Elements(); michael@0: for (uint32_t i = 0; i < samples; ++i) { michael@0: SetUint16LE(output + i*2, int16_t(input[i]*32767.0f)); michael@0: } michael@0: fwrite(output, 2, samples, aDumpFile); michael@0: fflush(aDumpFile); michael@0: } michael@0: michael@0: // NOTE: this must not block a LowLatency stream for any significant amount michael@0: // of time, or it will block the entirety of MSG michael@0: nsresult michael@0: AudioStream::Init(int32_t aNumChannels, int32_t aRate, michael@0: const dom::AudioChannel aAudioChannel, michael@0: LatencyRequest aLatencyRequest) michael@0: { michael@0: if (!GetCubebContext() || aNumChannels < 0 || aRate < 0) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: PR_LOG(gAudioStreamLog, PR_LOG_DEBUG, michael@0: ("%s channels: %d, rate: %d for %p", __FUNCTION__, aNumChannels, aRate, this)); michael@0: mInRate = mOutRate = aRate; michael@0: mChannels = aNumChannels; michael@0: mOutChannels = (aNumChannels > 2) ? 2 : aNumChannels; michael@0: mLatencyRequest = aLatencyRequest; michael@0: michael@0: mDumpFile = OpenDumpFile(this); michael@0: michael@0: cubeb_stream_params params; michael@0: params.rate = aRate; michael@0: params.channels = mOutChannels; michael@0: #if defined(__ANDROID__) michael@0: #if defined(MOZ_B2G) michael@0: params.stream_type = ConvertChannelToCubebType(aAudioChannel); michael@0: #else michael@0: params.stream_type = CUBEB_STREAM_TYPE_MUSIC; michael@0: #endif michael@0: michael@0: if (params.stream_type == CUBEB_STREAM_TYPE_MAX) { michael@0: return NS_ERROR_INVALID_ARG; michael@0: } michael@0: #endif michael@0: if (AUDIO_OUTPUT_FORMAT == AUDIO_FORMAT_S16) { michael@0: params.format = CUBEB_SAMPLE_S16NE; michael@0: } else { michael@0: params.format = CUBEB_SAMPLE_FLOAT32NE; michael@0: } michael@0: mBytesPerFrame = sizeof(AudioDataValue) * mOutChannels; michael@0: michael@0: mAudioClock.Init(); michael@0: michael@0: // Size mBuffer for one second of audio. This value is arbitrary, and was michael@0: // selected based on the observed behaviour of the existing AudioStream michael@0: // implementations. michael@0: uint32_t bufferLimit = FramesToBytes(aRate); michael@0: NS_ABORT_IF_FALSE(bufferLimit % mBytesPerFrame == 0, "Must buffer complete frames"); michael@0: mBuffer.SetCapacity(bufferLimit); michael@0: michael@0: if (aLatencyRequest == LowLatency) { michael@0: // Don't block this thread to initialize a cubeb stream. michael@0: // When this is done, it will start callbacks from Cubeb. Those will michael@0: // cause us to move from INITIALIZED to RUNNING. Until then, we michael@0: // can't access any cubeb functions. michael@0: // Use a RefPtr to avoid leaks if Dispatch fails michael@0: RefPtr init = new AudioInitTask(this, aLatencyRequest, params); michael@0: init->Dispatch(); michael@0: return NS_OK; michael@0: } michael@0: // High latency - open synchronously michael@0: nsresult rv = OpenCubeb(params, aLatencyRequest); michael@0: // See if we need to start() the stream, since we must do that from this michael@0: // thread for now (cubeb API issue) michael@0: CheckForStart(); michael@0: return rv; michael@0: } michael@0: michael@0: // This code used to live inside AudioStream::Init(), but on Mac (others?) michael@0: // it has been known to take 300-800 (or even 8500) ms to execute(!) michael@0: nsresult michael@0: AudioStream::OpenCubeb(cubeb_stream_params &aParams, michael@0: LatencyRequest aLatencyRequest) michael@0: { michael@0: cubeb* cubebContext = GetCubebContext(); michael@0: if (!cubebContext) { michael@0: MonitorAutoLock mon(mMonitor); michael@0: mState = AudioStream::ERRORED; michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: // If the latency pref is set, use it. Otherwise, if this stream is intended michael@0: // for low latency playback, try to get the lowest latency possible. michael@0: // Otherwise, for normal streams, use 100ms. michael@0: uint32_t latency; michael@0: if (aLatencyRequest == LowLatency && !CubebLatencyPrefSet()) { michael@0: if (cubeb_get_min_latency(cubebContext, aParams, &latency) != CUBEB_OK) { michael@0: latency = GetCubebLatency(); michael@0: } michael@0: } else { michael@0: latency = GetCubebLatency(); michael@0: } michael@0: michael@0: { michael@0: cubeb_stream* stream; michael@0: if (cubeb_stream_init(cubebContext, &stream, "AudioStream", aParams, michael@0: latency, DataCallback_S, StateCallback_S, this) == CUBEB_OK) { michael@0: MonitorAutoLock mon(mMonitor); michael@0: mCubebStream.own(stream); michael@0: // Make sure we weren't shut down while in flight! michael@0: if (mState == SHUTDOWN) { michael@0: mCubebStream.reset(); michael@0: LOG(("AudioStream::OpenCubeb() %p Shutdown while opening cubeb", this)); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: // We can't cubeb_stream_start() the thread from a transient thread due to michael@0: // cubeb API requirements (init can be called from another thread, but michael@0: // not start/stop/destroy/etc) michael@0: } else { michael@0: MonitorAutoLock mon(mMonitor); michael@0: mState = ERRORED; michael@0: LOG(("AudioStream::OpenCubeb() %p failed to init cubeb", this)); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: AudioStream::CheckForStart() michael@0: { michael@0: if (mState == INITIALIZED) { michael@0: // Start the stream right away when low latency has been requested. This means michael@0: // that the DataCallback will feed silence to cubeb, until the first frames michael@0: // are written to this AudioStream. Also start if a start has been queued. michael@0: if (mLatencyRequest == LowLatency || mNeedsStart) { michael@0: StartUnlocked(); // mState = STARTED or ERRORED michael@0: mNeedsStart = false; michael@0: PR_LOG(gAudioStreamLog, PR_LOG_WARNING, michael@0: ("Started waiting %s-latency stream", michael@0: mLatencyRequest == LowLatency ? "low" : "high")); michael@0: } else { michael@0: // high latency, not full - OR Pause() was called before we got here michael@0: PR_LOG(gAudioStreamLog, PR_LOG_DEBUG, michael@0: ("Not starting waiting %s-latency stream", michael@0: mLatencyRequest == LowLatency ? "low" : "high")); michael@0: } michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: AudioInitTask::Run() michael@0: { michael@0: MOZ_ASSERT(mThread); michael@0: if (NS_IsMainThread()) { michael@0: mThread->Shutdown(); // can't Shutdown from the thread itself, darn michael@0: // Don't null out mThread! michael@0: // See bug 999104. We must hold a ref to the thread across Dispatch() michael@0: // since the internal mThread ref could be released while processing michael@0: // the Dispatch(), and Dispatch/PutEvent itself doesn't hold a ref; it michael@0: // assumes the caller does. michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult rv = mAudioStream->OpenCubeb(mParams, mLatencyRequest); michael@0: michael@0: // and now kill this thread michael@0: NS_DispatchToMainThread(this); michael@0: return rv; michael@0: } michael@0: michael@0: // aTime is the time in ms the samples were inserted into MediaStreamGraph michael@0: nsresult michael@0: AudioStream::Write(const AudioDataValue* aBuf, uint32_t aFrames, TimeStamp *aTime) michael@0: { michael@0: MonitorAutoLock mon(mMonitor); michael@0: if (mState == ERRORED) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: NS_ASSERTION(mState == INITIALIZED || mState == STARTED || mState == RUNNING, michael@0: "Stream write in unexpected state."); michael@0: michael@0: // See if we need to start() the stream, since we must do that from this thread michael@0: CheckForStart(); michael@0: michael@0: // Downmix to Stereo. michael@0: if (mChannels > 2 && mChannels <= 8) { michael@0: DownmixAudioToStereo(const_cast (aBuf), mChannels, aFrames); michael@0: } michael@0: else if (mChannels > 8) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: const uint8_t* src = reinterpret_cast(aBuf); michael@0: uint32_t bytesToCopy = FramesToBytes(aFrames); michael@0: michael@0: // XXX this will need to change if we want to enable this on-the-fly! michael@0: if (PR_LOG_TEST(GetLatencyLog(), PR_LOG_DEBUG)) { michael@0: // Record the position and time this data was inserted michael@0: int64_t timeMs; michael@0: if (aTime && !aTime->IsNull()) { michael@0: if (mStartTime.IsNull()) { michael@0: AsyncLatencyLogger::Get(true)->GetStartTime(mStartTime); michael@0: } michael@0: timeMs = (*aTime - mStartTime).ToMilliseconds(); michael@0: } else { michael@0: timeMs = 0; michael@0: } michael@0: struct Inserts insert = { timeMs, aFrames}; michael@0: mInserts.AppendElement(insert); michael@0: } michael@0: michael@0: while (bytesToCopy > 0) { michael@0: uint32_t available = std::min(bytesToCopy, mBuffer.Available()); michael@0: NS_ABORT_IF_FALSE(available % mBytesPerFrame == 0, michael@0: "Must copy complete frames."); michael@0: michael@0: mBuffer.AppendElements(src, available); michael@0: src += available; michael@0: bytesToCopy -= available; michael@0: michael@0: if (bytesToCopy > 0) { michael@0: // Careful - the CubebInit thread may not have gotten to STARTED yet michael@0: if ((mState == INITIALIZED || mState == STARTED) && mLatencyRequest == LowLatency) { michael@0: // don't ever block MediaStreamGraph low-latency streams michael@0: uint32_t remains = 0; // we presume the buffer is full michael@0: if (mBuffer.Length() > bytesToCopy) { michael@0: remains = mBuffer.Length() - bytesToCopy; // Free up just enough space michael@0: } michael@0: // account for dropping samples michael@0: PR_LOG(gAudioStreamLog, PR_LOG_WARNING, ("Stream %p dropping %u bytes (%u frames)in Write()", michael@0: this, mBuffer.Length() - remains, BytesToFrames(mBuffer.Length() - remains))); michael@0: mReadPoint += BytesToFrames(mBuffer.Length() - remains); michael@0: mBuffer.ContractTo(remains); michael@0: } else { // RUNNING or high latency michael@0: // If we are not playing, but our buffer is full, start playing to make michael@0: // room for soon-to-be-decoded data. michael@0: if (mState != STARTED && mState != RUNNING) { michael@0: PR_LOG(gAudioStreamLog, PR_LOG_WARNING, ("Starting stream %p in Write (%u waiting)", michael@0: this, bytesToCopy)); michael@0: StartUnlocked(); michael@0: if (mState == ERRORED) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: } michael@0: PR_LOG(gAudioStreamLog, PR_LOG_WARNING, ("Stream %p waiting in Write() (%u waiting)", michael@0: this, bytesToCopy)); michael@0: mon.Wait(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: mWritten += aFrames; michael@0: return NS_OK; michael@0: } michael@0: michael@0: uint32_t michael@0: AudioStream::Available() michael@0: { michael@0: MonitorAutoLock mon(mMonitor); michael@0: NS_ABORT_IF_FALSE(mBuffer.Length() % mBytesPerFrame == 0, "Buffer invariant violated."); michael@0: return BytesToFrames(mBuffer.Available()); michael@0: } michael@0: michael@0: void michael@0: AudioStream::SetVolume(double aVolume) michael@0: { michael@0: MonitorAutoLock mon(mMonitor); michael@0: NS_ABORT_IF_FALSE(aVolume >= 0.0 && aVolume <= 1.0, "Invalid volume"); michael@0: mVolume = aVolume; michael@0: } michael@0: michael@0: void michael@0: AudioStream::Drain() michael@0: { michael@0: MonitorAutoLock mon(mMonitor); michael@0: LOG(("AudioStream::Drain() for %p, state %d, avail %u", this, mState, mBuffer.Available())); michael@0: if (mState != STARTED && mState != RUNNING) { michael@0: NS_ASSERTION(mState == ERRORED || mBuffer.Available() == 0, "Draining without full buffer of unplayed audio"); michael@0: return; michael@0: } michael@0: mState = DRAINING; michael@0: while (mState == DRAINING) { michael@0: mon.Wait(); michael@0: } michael@0: } michael@0: michael@0: void michael@0: AudioStream::Start() michael@0: { michael@0: MonitorAutoLock mon(mMonitor); michael@0: StartUnlocked(); michael@0: } michael@0: michael@0: void michael@0: AudioStream::StartUnlocked() michael@0: { michael@0: mMonitor.AssertCurrentThreadOwns(); michael@0: if (!mCubebStream) { michael@0: mNeedsStart = true; michael@0: return; michael@0: } michael@0: MonitorAutoUnlock mon(mMonitor); michael@0: if (mState == INITIALIZED) { michael@0: int r = cubeb_stream_start(mCubebStream); michael@0: mState = r == CUBEB_OK ? STARTED : ERRORED; michael@0: LOG(("AudioStream: started %p, state %s", this, mState == STARTED ? "STARTED" : "ERRORED")); michael@0: } michael@0: } michael@0: michael@0: void michael@0: AudioStream::Pause() michael@0: { michael@0: MonitorAutoLock mon(mMonitor); michael@0: if (!mCubebStream || (mState != STARTED && mState != RUNNING)) { michael@0: mNeedsStart = false; michael@0: mState = STOPPED; // which also tells async OpenCubeb not to start, just init michael@0: return; michael@0: } michael@0: michael@0: int r; michael@0: { michael@0: MonitorAutoUnlock mon(mMonitor); michael@0: r = cubeb_stream_stop(mCubebStream); michael@0: } michael@0: if (mState != ERRORED && r == CUBEB_OK) { michael@0: mState = STOPPED; michael@0: } michael@0: } michael@0: michael@0: void michael@0: AudioStream::Resume() michael@0: { michael@0: MonitorAutoLock mon(mMonitor); michael@0: if (!mCubebStream || mState != STOPPED) { michael@0: return; michael@0: } michael@0: michael@0: int r; michael@0: { michael@0: MonitorAutoUnlock mon(mMonitor); michael@0: r = cubeb_stream_start(mCubebStream); michael@0: } michael@0: if (mState != ERRORED && r == CUBEB_OK) { michael@0: mState = STARTED; michael@0: } michael@0: } michael@0: michael@0: void michael@0: AudioStream::Shutdown() michael@0: { michael@0: LOG(("AudioStream: Shutdown %p, state %d", this, mState)); michael@0: { michael@0: MonitorAutoLock mon(mMonitor); michael@0: if (mState == STARTED || mState == RUNNING) { michael@0: MonitorAutoUnlock mon(mMonitor); michael@0: Pause(); michael@0: } michael@0: MOZ_ASSERT(mState != STARTED && mState != RUNNING); // paranoia michael@0: mState = SHUTDOWN; michael@0: } michael@0: // Must not try to shut down cubeb from within the lock! wasapi may still michael@0: // call our callback after Pause()/stop()!?! Bug 996162 michael@0: if (mCubebStream) { michael@0: mCubebStream.reset(); michael@0: } michael@0: } michael@0: michael@0: int64_t michael@0: AudioStream::GetPosition() michael@0: { michael@0: MonitorAutoLock mon(mMonitor); michael@0: return mAudioClock.GetPositionUnlocked(); michael@0: } michael@0: michael@0: // This function is miscompiled by PGO with MSVC 2010. See bug 768333. michael@0: #ifdef _MSC_VER michael@0: #pragma optimize("", off) michael@0: #endif michael@0: int64_t michael@0: AudioStream::GetPositionInFrames() michael@0: { michael@0: return mAudioClock.GetPositionInFrames(); michael@0: } michael@0: #ifdef _MSC_VER michael@0: #pragma optimize("", on) michael@0: #endif michael@0: michael@0: int64_t michael@0: AudioStream::GetPositionInFramesInternal() michael@0: { michael@0: MonitorAutoLock mon(mMonitor); michael@0: return GetPositionInFramesUnlocked(); michael@0: } michael@0: michael@0: int64_t michael@0: AudioStream::GetPositionInFramesUnlocked() michael@0: { michael@0: mMonitor.AssertCurrentThreadOwns(); michael@0: michael@0: if (!mCubebStream || mState == ERRORED) { michael@0: return -1; michael@0: } michael@0: michael@0: uint64_t position = 0; michael@0: { michael@0: MonitorAutoUnlock mon(mMonitor); michael@0: if (cubeb_stream_get_position(mCubebStream, &position) != CUBEB_OK) { michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: // Adjust the reported position by the number of silent frames written michael@0: // during stream underruns. michael@0: uint64_t adjustedPosition = 0; michael@0: if (position >= mLostFrames) { michael@0: adjustedPosition = position - mLostFrames; michael@0: } michael@0: return std::min(adjustedPosition, INT64_MAX); michael@0: } michael@0: michael@0: int64_t michael@0: AudioStream::GetLatencyInFrames() michael@0: { michael@0: uint32_t latency; michael@0: if (cubeb_stream_get_latency(mCubebStream, &latency)) { michael@0: NS_WARNING("Could not get cubeb latency."); michael@0: return 0; michael@0: } michael@0: return static_cast(latency); michael@0: } michael@0: michael@0: bool michael@0: AudioStream::IsPaused() michael@0: { michael@0: MonitorAutoLock mon(mMonitor); michael@0: return mState == STOPPED; michael@0: } michael@0: michael@0: void michael@0: AudioStream::GetBufferInsertTime(int64_t &aTimeMs) michael@0: { michael@0: if (mInserts.Length() > 0) { michael@0: // Find the right block, but don't leave the array empty michael@0: while (mInserts.Length() > 1 && mReadPoint >= mInserts[0].mFrames) { michael@0: mReadPoint -= mInserts[0].mFrames; michael@0: mInserts.RemoveElementAt(0); michael@0: } michael@0: // offset for amount already read michael@0: // XXX Note: could misreport if we couldn't find a block in the right timeframe michael@0: aTimeMs = mInserts[0].mTimeMs + ((mReadPoint * 1000) / mOutRate); michael@0: } else { michael@0: aTimeMs = INT64_MAX; michael@0: } michael@0: } michael@0: michael@0: long michael@0: AudioStream::GetUnprocessed(void* aBuffer, long aFrames, int64_t &aTimeMs) michael@0: { michael@0: uint8_t* wpos = reinterpret_cast(aBuffer); michael@0: michael@0: // Flush the timestretcher pipeline, if we were playing using a playback rate michael@0: // other than 1.0. michael@0: uint32_t flushedFrames = 0; michael@0: if (mTimeStretcher && mTimeStretcher->numSamples()) { michael@0: flushedFrames = mTimeStretcher->receiveSamples(reinterpret_cast(wpos), aFrames); michael@0: wpos += FramesToBytes(flushedFrames); michael@0: } michael@0: uint32_t toPopBytes = FramesToBytes(aFrames - flushedFrames); michael@0: uint32_t available = std::min(toPopBytes, mBuffer.Length()); michael@0: michael@0: void* input[2]; michael@0: uint32_t input_size[2]; michael@0: mBuffer.PopElements(available, &input[0], &input_size[0], &input[1], &input_size[1]); michael@0: memcpy(wpos, input[0], input_size[0]); michael@0: wpos += input_size[0]; michael@0: memcpy(wpos, input[1], input_size[1]); michael@0: michael@0: // First time block now has our first returned sample michael@0: mReadPoint += BytesToFrames(available); michael@0: GetBufferInsertTime(aTimeMs); michael@0: michael@0: return BytesToFrames(available) + flushedFrames; michael@0: } michael@0: michael@0: // Get unprocessed samples, and pad the beginning of the buffer with silence if michael@0: // there is not enough data. michael@0: long michael@0: AudioStream::GetUnprocessedWithSilencePadding(void* aBuffer, long aFrames, int64_t& aTimeMs) michael@0: { michael@0: uint32_t toPopBytes = FramesToBytes(aFrames); michael@0: uint32_t available = std::min(toPopBytes, mBuffer.Length()); michael@0: uint32_t silenceOffset = toPopBytes - available; michael@0: michael@0: uint8_t* wpos = reinterpret_cast(aBuffer); michael@0: michael@0: memset(wpos, 0, silenceOffset); michael@0: wpos += silenceOffset; michael@0: michael@0: void* input[2]; michael@0: uint32_t input_size[2]; michael@0: mBuffer.PopElements(available, &input[0], &input_size[0], &input[1], &input_size[1]); michael@0: memcpy(wpos, input[0], input_size[0]); michael@0: wpos += input_size[0]; michael@0: memcpy(wpos, input[1], input_size[1]); michael@0: michael@0: GetBufferInsertTime(aTimeMs); michael@0: michael@0: return aFrames; michael@0: } michael@0: michael@0: long michael@0: AudioStream::GetTimeStretched(void* aBuffer, long aFrames, int64_t &aTimeMs) michael@0: { michael@0: long processedFrames = 0; michael@0: michael@0: // We need to call the non-locking version, because we already have the lock. michael@0: if (EnsureTimeStretcherInitializedUnlocked() != NS_OK) { michael@0: return 0; michael@0: } michael@0: michael@0: uint8_t* wpos = reinterpret_cast(aBuffer); michael@0: double playbackRate = static_cast(mInRate) / mOutRate; michael@0: uint32_t toPopBytes = FramesToBytes(ceil(aFrames / playbackRate)); michael@0: uint32_t available = 0; michael@0: bool lowOnBufferedData = false; michael@0: do { michael@0: // Check if we already have enough data in the time stretcher pipeline. michael@0: if (mTimeStretcher->numSamples() <= static_cast(aFrames)) { michael@0: void* input[2]; michael@0: uint32_t input_size[2]; michael@0: available = std::min(mBuffer.Length(), toPopBytes); michael@0: if (available != toPopBytes) { michael@0: lowOnBufferedData = true; michael@0: } michael@0: mBuffer.PopElements(available, &input[0], &input_size[0], michael@0: &input[1], &input_size[1]); michael@0: mReadPoint += BytesToFrames(available); michael@0: for(uint32_t i = 0; i < 2; i++) { michael@0: mTimeStretcher->putSamples(reinterpret_cast(input[i]), BytesToFrames(input_size[i])); michael@0: } michael@0: } michael@0: uint32_t receivedFrames = mTimeStretcher->receiveSamples(reinterpret_cast(wpos), aFrames - processedFrames); michael@0: wpos += FramesToBytes(receivedFrames); michael@0: processedFrames += receivedFrames; michael@0: } while (processedFrames < aFrames && !lowOnBufferedData); michael@0: michael@0: GetBufferInsertTime(aTimeMs); michael@0: michael@0: return processedFrames; michael@0: } michael@0: michael@0: long michael@0: AudioStream::DataCallback(void* aBuffer, long aFrames) michael@0: { michael@0: MonitorAutoLock mon(mMonitor); michael@0: uint32_t available = std::min(static_cast(FramesToBytes(aFrames)), mBuffer.Length()); michael@0: NS_ABORT_IF_FALSE(available % mBytesPerFrame == 0, "Must copy complete frames"); michael@0: AudioDataValue* output = reinterpret_cast(aBuffer); michael@0: uint32_t underrunFrames = 0; michael@0: uint32_t servicedFrames = 0; michael@0: int64_t insertTime; michael@0: michael@0: // NOTE: wasapi (others?) can call us back *after* stop()/Shutdown() (mState == SHUTDOWN) michael@0: // Bug 996162 michael@0: michael@0: // callback tells us cubeb succeeded initializing michael@0: if (mState == STARTED) { michael@0: // For low-latency streams, we want to minimize any built-up data when michael@0: // we start getting callbacks. michael@0: // Simple version - contract on first callback only. michael@0: if (mLatencyRequest == LowLatency) { michael@0: #ifdef PR_LOGGING michael@0: uint32_t old_len = mBuffer.Length(); michael@0: #endif michael@0: available = mBuffer.ContractTo(FramesToBytes(aFrames)); michael@0: #ifdef PR_LOGGING michael@0: TimeStamp now = TimeStamp::Now(); michael@0: if (!mStartTime.IsNull()) { michael@0: int64_t timeMs = (now - mStartTime).ToMilliseconds(); michael@0: PR_LOG(gAudioStreamLog, PR_LOG_WARNING, michael@0: ("Stream took %lldms to start after first Write() @ %u", timeMs, mOutRate)); michael@0: } else { michael@0: PR_LOG(gAudioStreamLog, PR_LOG_WARNING, michael@0: ("Stream started before Write() @ %u", mOutRate)); michael@0: } michael@0: michael@0: if (old_len != available) { michael@0: // Note that we may have dropped samples in Write() as well! michael@0: PR_LOG(gAudioStreamLog, PR_LOG_WARNING, michael@0: ("AudioStream %p dropped %u + %u initial frames @ %u", this, michael@0: mReadPoint, BytesToFrames(old_len - available), mOutRate)); michael@0: mReadPoint += BytesToFrames(old_len - available); michael@0: } michael@0: #endif michael@0: } michael@0: mState = RUNNING; michael@0: } michael@0: michael@0: if (available) { michael@0: // When we are playing a low latency stream, and it is the first time we are michael@0: // getting data from the buffer, we prefer to add the silence for an michael@0: // underrun at the beginning of the buffer, so the first buffer is not cut michael@0: // in half by the silence inserted to compensate for the underrun. michael@0: if (mInRate == mOutRate) { michael@0: if (mLatencyRequest == LowLatency && !mWritten) { michael@0: servicedFrames = GetUnprocessedWithSilencePadding(output, aFrames, insertTime); michael@0: } else { michael@0: servicedFrames = GetUnprocessed(output, aFrames, insertTime); michael@0: } michael@0: } else { michael@0: servicedFrames = GetTimeStretched(output, aFrames, insertTime); michael@0: } michael@0: float scaled_volume = float(GetVolumeScale() * mVolume); michael@0: michael@0: ScaleAudioSamples(output, aFrames * mOutChannels, scaled_volume); michael@0: michael@0: NS_ABORT_IF_FALSE(mBuffer.Length() % mBytesPerFrame == 0, "Must copy complete frames"); michael@0: michael@0: // Notify any blocked Write() call that more space is available in mBuffer. michael@0: mon.NotifyAll(); michael@0: } else { michael@0: GetBufferInsertTime(insertTime); michael@0: } michael@0: michael@0: underrunFrames = aFrames - servicedFrames; michael@0: michael@0: if (mState != DRAINING) { michael@0: uint8_t* rpos = static_cast(aBuffer) + FramesToBytes(aFrames - underrunFrames); michael@0: memset(rpos, 0, FramesToBytes(underrunFrames)); michael@0: if (underrunFrames) { michael@0: PR_LOG(gAudioStreamLog, PR_LOG_WARNING, michael@0: ("AudioStream %p lost %d frames", this, underrunFrames)); michael@0: } michael@0: mLostFrames += underrunFrames; michael@0: servicedFrames += underrunFrames; michael@0: } michael@0: michael@0: WriteDumpFile(mDumpFile, this, aFrames, aBuffer); michael@0: // Don't log if we're not interested or if the stream is inactive michael@0: if (PR_LOG_TEST(GetLatencyLog(), PR_LOG_DEBUG) && michael@0: mState != SHUTDOWN && michael@0: insertTime != INT64_MAX && servicedFrames > underrunFrames) { michael@0: uint32_t latency = UINT32_MAX; michael@0: if (cubeb_stream_get_latency(mCubebStream, &latency)) { michael@0: NS_WARNING("Could not get latency from cubeb."); michael@0: } michael@0: TimeStamp now = TimeStamp::Now(); michael@0: michael@0: mLatencyLog->Log(AsyncLatencyLogger::AudioStream, reinterpret_cast(this), michael@0: insertTime, now); michael@0: mLatencyLog->Log(AsyncLatencyLogger::Cubeb, reinterpret_cast(mCubebStream.get()), michael@0: (latency * 1000) / mOutRate, now); michael@0: } michael@0: michael@0: mAudioClock.UpdateWritePosition(servicedFrames); michael@0: return servicedFrames; michael@0: } michael@0: michael@0: void michael@0: AudioStream::StateCallback(cubeb_state aState) michael@0: { michael@0: MonitorAutoLock mon(mMonitor); michael@0: if (aState == CUBEB_STATE_DRAINED) { michael@0: mState = DRAINED; michael@0: } else if (aState == CUBEB_STATE_ERROR) { michael@0: LOG(("AudioStream::StateCallback() state %d cubeb error", mState)); michael@0: mState = ERRORED; michael@0: } michael@0: mon.NotifyAll(); michael@0: } michael@0: michael@0: AudioClock::AudioClock(AudioStream* aStream) michael@0: :mAudioStream(aStream), michael@0: mOldOutRate(0), michael@0: mBasePosition(0), michael@0: mBaseOffset(0), michael@0: mOldBaseOffset(0), michael@0: mOldBasePosition(0), michael@0: mPlaybackRateChangeOffset(0), michael@0: mPreviousPosition(0), michael@0: mWritten(0), michael@0: mOutRate(0), michael@0: mInRate(0), michael@0: mPreservesPitch(true), michael@0: mCompensatingLatency(false) michael@0: {} michael@0: michael@0: void AudioClock::Init() michael@0: { michael@0: mOutRate = mAudioStream->GetRate(); michael@0: mInRate = mAudioStream->GetRate(); michael@0: mOldOutRate = mOutRate; michael@0: } michael@0: michael@0: void AudioClock::UpdateWritePosition(uint32_t aCount) michael@0: { michael@0: mWritten += aCount; michael@0: } michael@0: michael@0: uint64_t AudioClock::GetPositionUnlocked() michael@0: { michael@0: // GetPositionInFramesUnlocked() asserts it owns the monitor michael@0: int64_t position = mAudioStream->GetPositionInFramesUnlocked(); michael@0: int64_t diffOffset; michael@0: NS_ASSERTION(position < 0 || (mInRate != 0 && mOutRate != 0), "AudioClock not initialized."); michael@0: if (position >= 0) { michael@0: if (position < mPlaybackRateChangeOffset) { michael@0: // See if we are still playing frames pushed with the old playback rate in michael@0: // the backend. If we are, use the old output rate to compute the michael@0: // position. michael@0: mCompensatingLatency = true; michael@0: diffOffset = position - mOldBaseOffset; michael@0: position = static_cast(mOldBasePosition + michael@0: static_cast(USECS_PER_S * diffOffset) / mOldOutRate); michael@0: mPreviousPosition = position; michael@0: return position; michael@0: } michael@0: michael@0: if (mCompensatingLatency) { michael@0: diffOffset = position - mPlaybackRateChangeOffset; michael@0: mCompensatingLatency = false; michael@0: mBasePosition = mPreviousPosition; michael@0: } else { michael@0: diffOffset = position - mPlaybackRateChangeOffset; michael@0: } michael@0: position = static_cast(mBasePosition + michael@0: (static_cast(USECS_PER_S * diffOffset) / mOutRate)); michael@0: return position; michael@0: } michael@0: return UINT64_MAX; michael@0: } michael@0: michael@0: uint64_t AudioClock::GetPositionInFrames() michael@0: { michael@0: return (GetPositionUnlocked() * mOutRate) / USECS_PER_S; michael@0: } michael@0: michael@0: void AudioClock::SetPlaybackRateUnlocked(double aPlaybackRate) michael@0: { michael@0: // GetPositionInFramesUnlocked() asserts it owns the monitor michael@0: int64_t position = mAudioStream->GetPositionInFramesUnlocked(); michael@0: if (position > mPlaybackRateChangeOffset) { michael@0: mOldBasePosition = mBasePosition; michael@0: mBasePosition = GetPositionUnlocked(); michael@0: mOldBaseOffset = mPlaybackRateChangeOffset; michael@0: mBaseOffset = position; michael@0: mPlaybackRateChangeOffset = mWritten; michael@0: mOldOutRate = mOutRate; michael@0: mOutRate = static_cast(mInRate / aPlaybackRate); michael@0: } else { michael@0: // The playbackRate has been changed before the end of the latency michael@0: // compensation phase. We don't update the mOld* variable. That way, the michael@0: // last playbackRate set is taken into account. michael@0: mBasePosition = GetPositionUnlocked(); michael@0: mBaseOffset = position; michael@0: mPlaybackRateChangeOffset = mWritten; michael@0: mOutRate = static_cast(mInRate / aPlaybackRate); michael@0: } michael@0: } michael@0: michael@0: double AudioClock::GetPlaybackRate() michael@0: { michael@0: return static_cast(mInRate) / mOutRate; michael@0: } michael@0: michael@0: void AudioClock::SetPreservesPitch(bool aPreservesPitch) michael@0: { michael@0: mPreservesPitch = aPreservesPitch; michael@0: } michael@0: michael@0: bool AudioClock::GetPreservesPitch() michael@0: { michael@0: return mPreservesPitch; michael@0: } michael@0: } // namespace mozilla