2013-12-11 09:03:30 +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: */
|
|
|
|
/* 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/. */
|
2015-07-23 14:58:30 +03:00
|
|
|
|
2016-02-05 05:19:12 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2015-07-23 14:58:30 +03:00
|
|
|
#include "MediaQueue.h"
|
2015-08-18 06:55:01 +03:00
|
|
|
#include "DecodedAudioDataSink.h"
|
2015-07-23 14:58:30 +03:00
|
|
|
#include "VideoUtils.h"
|
2016-04-11 14:16:17 +03:00
|
|
|
#include "AudioConverter.h"
|
2015-07-23 14:58:30 +03:00
|
|
|
|
|
|
|
#include "mozilla/CheckedInt.h"
|
2015-07-28 06:52:05 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2016-04-12 12:24:09 +03:00
|
|
|
#include "gfxPrefs.h"
|
2013-12-11 09:03:30 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-11-15 16:49:01 +03:00
|
|
|
extern LazyLogModule gMediaDecoderLog;
|
2013-12-11 09:03:30 +04:00
|
|
|
#define SINK_LOG(msg, ...) \
|
2015-08-18 06:55:01 +03:00
|
|
|
MOZ_LOG(gMediaDecoderLog, LogLevel::Debug, \
|
|
|
|
("DecodedAudioDataSink=%p " msg, this, ##__VA_ARGS__))
|
2015-05-14 05:15:05 +03:00
|
|
|
#define SINK_LOG_V(msg, ...) \
|
2015-08-18 06:55:01 +03:00
|
|
|
MOZ_LOG(gMediaDecoderLog, LogLevel::Verbose, \
|
|
|
|
("DecodedAudioDataSink=%p " msg, this, ##__VA_ARGS__))
|
|
|
|
|
|
|
|
namespace media {
|
2013-12-11 09:03:30 +04:00
|
|
|
|
2014-09-23 00:26:36 +04:00
|
|
|
// The amount of audio frames that is used to fuzz rounding errors.
|
|
|
|
static const int64_t AUDIO_FUZZ_FRAMES = 1;
|
|
|
|
|
2016-04-13 10:17:54 +03:00
|
|
|
// Amount of audio frames we will be processing ahead of use
|
|
|
|
static const int32_t LOW_AUDIO_USECS = 300000;
|
|
|
|
|
|
|
|
DecodedAudioDataSink::DecodedAudioDataSink(AbstractThread* aThread,
|
|
|
|
MediaQueue<MediaData>& aAudioQueue,
|
2015-08-18 06:55:01 +03:00
|
|
|
int64_t aStartTime,
|
|
|
|
const AudioInfo& aInfo,
|
|
|
|
dom::AudioChannel aChannel)
|
|
|
|
: AudioSink(aAudioQueue)
|
2013-12-11 09:03:30 +04:00
|
|
|
, mStartTime(aStartTime)
|
|
|
|
, mWritten(0)
|
2014-10-30 23:10:00 +03:00
|
|
|
, mLastGoodPosition(0)
|
2013-12-11 09:03:30 +04:00
|
|
|
, mInfo(aInfo)
|
|
|
|
, mChannel(aChannel)
|
|
|
|
, mPlaying(true)
|
2016-04-13 10:17:54 +03:00
|
|
|
, mErrored(false)
|
2016-03-22 10:55:51 +03:00
|
|
|
, mPlaybackComplete(false)
|
2016-04-13 10:17:54 +03:00
|
|
|
, mOwnerThread(aThread)
|
|
|
|
, mProcessedQueueLength(0)
|
|
|
|
, mFramesParsed(0)
|
|
|
|
, mLastEndTime(0)
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2016-04-12 12:24:09 +03:00
|
|
|
bool resampling = gfxPrefs::AudioSinkResampling();
|
2016-04-21 17:31:58 +03:00
|
|
|
uint32_t resamplingRate = gfxPrefs::AudioSinkResampleRate();
|
2016-04-13 10:17:54 +03:00
|
|
|
mOutputRate = resampling ? resamplingRate : mInfo.mRate;
|
|
|
|
mOutputChannels = mInfo.mChannels > 2 && gfxPrefs::AudioSinkForceStereo()
|
|
|
|
? 2 : mInfo.mChannels;
|
2016-04-21 17:32:00 +03:00
|
|
|
mConverter =
|
|
|
|
MakeUnique<AudioConverter>(
|
|
|
|
AudioConfig(mInfo.mChannels, mInfo.mRate),
|
2016-04-13 10:17:54 +03:00
|
|
|
AudioConfig(mOutputChannels, mOutputRate));
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
2015-08-26 05:30:56 +03:00
|
|
|
DecodedAudioDataSink::~DecodedAudioDataSink()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GenericPromise>
|
2016-01-12 16:48:25 +03:00
|
|
|
DecodedAudioDataSink::Init(const PlaybackParams& aParams)
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2016-04-13 10:17:54 +03:00
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
|
|
|
|
|
|
|
mAudioQueueListener = mAudioQueue.PushEvent().Connect(
|
|
|
|
mOwnerThread, this, &DecodedAudioDataSink::OnAudioPushed);
|
|
|
|
mAudioQueueFinishListener = mAudioQueue.FinishEvent().Connect(
|
|
|
|
mOwnerThread, this, &DecodedAudioDataSink::NotifyAudioNeeded);
|
|
|
|
mProcessedQueueListener = mProcessedQueue.PopEvent().Connect(
|
|
|
|
mOwnerThread, this, &DecodedAudioDataSink::OnAudioPopped);
|
|
|
|
|
|
|
|
// To ensure at least one audio packet will be popped from AudioQueue and
|
|
|
|
// ready to be played.
|
|
|
|
NotifyAudioNeeded();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GenericPromise> p = mEndPromise.Ensure(__func__);
|
2016-01-12 16:48:25 +03:00
|
|
|
nsresult rv = InitializeAudioStream(aParams);
|
2013-12-11 09:03:30 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2015-07-22 04:54:06 +03:00
|
|
|
mEndPromise.Reject(rv, __func__);
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
2015-07-22 04:54:06 +03:00
|
|
|
return p;
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::GetPosition()
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2014-10-30 23:10:00 +03:00
|
|
|
int64_t pos;
|
|
|
|
if (mAudioStream &&
|
|
|
|
(pos = mAudioStream->GetPosition()) >= 0) {
|
2015-11-02 16:26:12 +03:00
|
|
|
NS_ASSERTION(pos >= mLastGoodPosition,
|
|
|
|
"AudioStream position shouldn't go backward");
|
2014-10-30 23:10:00 +03:00
|
|
|
// Update the last good position when we got a good one.
|
2015-11-02 16:26:12 +03:00
|
|
|
if (pos >= mLastGoodPosition) {
|
|
|
|
mLastGoodPosition = pos;
|
|
|
|
}
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
2014-10-30 23:10:00 +03:00
|
|
|
|
2015-07-09 06:07:57 +03:00
|
|
|
return mStartTime + mLastGoodPosition;
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
2015-02-27 00:37:03 +03:00
|
|
|
bool
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::HasUnplayedFrames()
|
2015-02-27 00:37:03 +03:00
|
|
|
{
|
|
|
|
// Experimentation suggests that GetPositionInFrames() is zero-indexed,
|
|
|
|
// so we need to add 1 here before comparing it to mWritten.
|
2016-04-13 10:17:54 +03:00
|
|
|
return mProcessedQueue.GetSize() ||
|
|
|
|
(mAudioStream && mAudioStream->GetPositionInFrames() + 1 < mWritten);
|
2015-02-27 00:37:03 +03:00
|
|
|
}
|
|
|
|
|
2013-12-11 09:03:30 +04:00
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::Shutdown()
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2016-04-13 10:17:54 +03:00
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
|
|
|
|
|
|
|
mAudioQueueListener.Disconnect();
|
|
|
|
mAudioQueueFinishListener.Disconnect();
|
|
|
|
mProcessedQueueListener.Disconnect();
|
|
|
|
|
2015-07-22 04:54:15 +03:00
|
|
|
if (mAudioStream) {
|
|
|
|
mAudioStream->Shutdown();
|
|
|
|
mAudioStream = nullptr;
|
|
|
|
}
|
2016-04-13 10:17:54 +03:00
|
|
|
mProcessedQueue.Reset();
|
|
|
|
mProcessedQueue.Finish();
|
2016-01-12 16:48:25 +03:00
|
|
|
mEndPromise.ResolveIfExists(true, __func__);
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::SetVolume(double aVolume)
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2016-01-12 16:48:25 +03:00
|
|
|
if (mAudioStream) {
|
|
|
|
mAudioStream->SetVolume(aVolume);
|
|
|
|
}
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::SetPlaybackRate(double aPlaybackRate)
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2015-07-31 08:26:06 +03:00
|
|
|
MOZ_ASSERT(aPlaybackRate != 0, "Don't set the playbackRate to 0 on AudioStream");
|
2016-01-12 16:48:25 +03:00
|
|
|
if (mAudioStream) {
|
|
|
|
mAudioStream->SetPlaybackRate(aPlaybackRate);
|
|
|
|
}
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::SetPreservesPitch(bool aPreservesPitch)
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2016-01-12 16:48:25 +03:00
|
|
|
if (mAudioStream) {
|
|
|
|
mAudioStream->SetPreservesPitch(aPreservesPitch);
|
|
|
|
}
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-18 06:55:01 +03:00
|
|
|
DecodedAudioDataSink::SetPlaying(bool aPlaying)
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2016-03-22 10:55:51 +03:00
|
|
|
if (!mAudioStream || mPlaying == aPlaying || mPlaybackComplete) {
|
2016-01-12 16:48:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// pause/resume AudioStream as necessary.
|
|
|
|
if (!aPlaying && !mAudioStream->IsPaused()) {
|
|
|
|
mAudioStream->Pause();
|
|
|
|
} else if (aPlaying && mAudioStream->IsPaused()) {
|
|
|
|
mAudioStream->Resume();
|
|
|
|
}
|
|
|
|
mPlaying = aPlaying;
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-01-12 16:48:25 +03:00
|
|
|
DecodedAudioDataSink::InitializeAudioStream(const PlaybackParams& aParams)
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2016-01-12 16:48:25 +03:00
|
|
|
mAudioStream = new AudioStream(*this);
|
2016-04-13 10:17:54 +03:00
|
|
|
nsresult rv = mAudioStream->Init(mOutputChannels, mOutputRate, mChannel);
|
2014-09-29 09:31:00 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-01-12 16:48:25 +03:00
|
|
|
mAudioStream->Shutdown();
|
|
|
|
mAudioStream = nullptr;
|
2014-09-29 09:31:00 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-01-12 16:48:25 +03:00
|
|
|
// Set playback params before calling Start() so they can take effect
|
|
|
|
// as soon as the 1st DataCallback of the AudioStream fires.
|
|
|
|
mAudioStream->SetVolume(aParams.mVolume);
|
|
|
|
mAudioStream->SetPlaybackRate(aParams.mPlaybackRate);
|
|
|
|
mAudioStream->SetPreservesPitch(aParams.mPreservesPitch);
|
2013-12-11 09:03:30 +04:00
|
|
|
mAudioStream->Start();
|
|
|
|
|
2016-01-12 16:48:25 +03:00
|
|
|
return NS_OK;
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
2016-01-12 16:48:25 +03:00
|
|
|
int64_t
|
|
|
|
DecodedAudioDataSink::GetEndTime() const
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2016-04-13 10:17:54 +03:00
|
|
|
CheckedInt64 playedUsecs = FramesToUsecs(mWritten, mOutputRate) + mStartTime;
|
2016-01-12 16:48:25 +03:00
|
|
|
if (!playedUsecs.isValid()) {
|
|
|
|
NS_WARNING("Int overflow calculating audio end time");
|
|
|
|
return -1;
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
2016-04-13 10:17:54 +03:00
|
|
|
// As we may be resampling, rounding errors may occur. Ensure we never get
|
|
|
|
// past the original end time.
|
|
|
|
return std::min<int64_t>(mLastEndTime, playedUsecs.value());
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
2016-01-12 16:48:25 +03:00
|
|
|
UniquePtr<AudioStream::Chunk>
|
|
|
|
DecodedAudioDataSink::PopFrames(uint32_t aFrames)
|
2015-07-28 06:52:05 +03:00
|
|
|
{
|
2016-01-12 16:48:25 +03:00
|
|
|
class Chunk : public AudioStream::Chunk {
|
|
|
|
public:
|
2016-01-18 06:24:06 +03:00
|
|
|
Chunk(AudioData* aBuffer, uint32_t aFrames, AudioDataValue* aData)
|
|
|
|
: mBuffer(aBuffer), mFrames(aFrames), mData(aData) {}
|
2016-01-12 16:48:25 +03:00
|
|
|
Chunk() : mFrames(0), mData(nullptr) {}
|
|
|
|
const AudioDataValue* Data() const { return mData; }
|
|
|
|
uint32_t Frames() const { return mFrames; }
|
2016-01-21 16:11:14 +03:00
|
|
|
uint32_t Channels() const { return mBuffer ? mBuffer->mChannels: 0; }
|
|
|
|
uint32_t Rate() const { return mBuffer ? mBuffer->mRate : 0; }
|
2016-01-12 16:48:25 +03:00
|
|
|
AudioDataValue* GetWritable() const { return mData; }
|
|
|
|
private:
|
|
|
|
const RefPtr<AudioData> mBuffer;
|
|
|
|
const uint32_t mFrames;
|
|
|
|
AudioDataValue* const mData;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SilentChunk : public AudioStream::Chunk {
|
|
|
|
public:
|
2016-01-21 16:11:14 +03:00
|
|
|
SilentChunk(uint32_t aFrames, uint32_t aChannels, uint32_t aRate)
|
2016-01-12 16:48:25 +03:00
|
|
|
: mFrames(aFrames)
|
2016-01-21 16:11:14 +03:00
|
|
|
, mChannels(aChannels)
|
|
|
|
, mRate(aRate)
|
2016-01-12 16:48:25 +03:00
|
|
|
, mData(MakeUnique<AudioDataValue[]>(aChannels * aFrames)) {
|
|
|
|
memset(mData.get(), 0, aChannels * aFrames * sizeof(AudioDataValue));
|
|
|
|
}
|
|
|
|
const AudioDataValue* Data() const { return mData.get(); }
|
|
|
|
uint32_t Frames() const { return mFrames; }
|
2016-01-21 16:11:14 +03:00
|
|
|
uint32_t Channels() const { return mChannels; }
|
|
|
|
uint32_t Rate() const { return mRate; }
|
2016-01-12 16:48:25 +03:00
|
|
|
AudioDataValue* GetWritable() const { return mData.get(); }
|
|
|
|
private:
|
|
|
|
const uint32_t mFrames;
|
2016-01-21 16:11:14 +03:00
|
|
|
const uint32_t mChannels;
|
|
|
|
const uint32_t mRate;
|
2016-01-12 16:48:25 +03:00
|
|
|
UniquePtr<AudioDataValue[]> mData;
|
|
|
|
};
|
|
|
|
|
2016-04-13 10:17:54 +03:00
|
|
|
bool needPopping = false;
|
|
|
|
if (!mCurrentData) {
|
2016-01-12 16:48:25 +03:00
|
|
|
// No data in the queue. Return an empty chunk.
|
2016-04-13 10:17:54 +03:00
|
|
|
if (!mProcessedQueue.GetSize()) {
|
2016-04-21 17:32:21 +03:00
|
|
|
return MakeUnique<Chunk>();
|
|
|
|
}
|
|
|
|
|
2016-04-13 10:17:54 +03:00
|
|
|
// We need to update our values prior popping the processed queue in
|
|
|
|
// order to prevent the pop event to fire too early (prior
|
|
|
|
// mProcessedQueueLength being updated) or prevent HasUnplayedFrames
|
|
|
|
// to incorrectly return true during the time interval betweeen the
|
|
|
|
// when mProcessedQueue is read and mWritten is updated.
|
|
|
|
needPopping = true;
|
|
|
|
mCurrentData = mProcessedQueue.PeekFront();
|
|
|
|
mCursor = MakeUnique<AudioBufferCursor>(mCurrentData->mAudioData.get(),
|
|
|
|
mCurrentData->mChannels,
|
|
|
|
mCurrentData->mFrames);
|
|
|
|
MOZ_ASSERT(mCurrentData->mFrames > 0);
|
|
|
|
mProcessedQueueLength -=
|
|
|
|
FramesToUsecs(mCurrentData->mFrames, mOutputRate).value();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto framesToPop = std::min(aFrames, mCursor->Available());
|
|
|
|
|
|
|
|
SINK_LOG_V("playing audio at time=%lld offset=%u length=%u",
|
|
|
|
mCurrentData->mTime, mCurrentData->mFrames - mCursor->Available(), framesToPop);
|
|
|
|
|
|
|
|
UniquePtr<AudioStream::Chunk> chunk =
|
|
|
|
MakeUnique<Chunk>(mCurrentData, framesToPop, mCursor->Ptr());
|
|
|
|
|
|
|
|
mWritten += framesToPop;
|
|
|
|
mCursor->Advance(framesToPop);
|
|
|
|
|
|
|
|
// All frames are popped. Reset mCurrentData so we can pop new elements from
|
|
|
|
// the audio queue in next calls to PopFrames().
|
|
|
|
if (!mCursor->Available()) {
|
|
|
|
mCurrentData = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needPopping) {
|
|
|
|
// We can now safely pop the audio packet from the processed queue.
|
|
|
|
// This will fire the popped event, triggering a call to NotifyAudioNeeded.
|
|
|
|
RefPtr<AudioData> releaseMe = mProcessedQueue.PopFront();
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DecodedAudioDataSink::Ended() const
|
|
|
|
{
|
|
|
|
// Return true when error encountered so AudioStream can start draining.
|
|
|
|
return mProcessedQueue.IsFinished() || mErrored;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DecodedAudioDataSink::Drained()
|
|
|
|
{
|
|
|
|
SINK_LOG("Drained");
|
|
|
|
mPlaybackComplete = true;
|
|
|
|
mEndPromise.ResolveIfExists(true, __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DecodedAudioDataSink::OnAudioPopped(const RefPtr<MediaData>& aSample)
|
|
|
|
{
|
|
|
|
SINK_LOG_V("AudioStream has used an audio packet.");
|
|
|
|
NotifyAudioNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DecodedAudioDataSink::OnAudioPushed(const RefPtr<MediaData>& aSample)
|
|
|
|
{
|
|
|
|
SINK_LOG_V("One new audio packet available.");
|
|
|
|
NotifyAudioNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DecodedAudioDataSink::NotifyAudioNeeded()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn(),
|
|
|
|
"Not called from the owner's thread");
|
|
|
|
|
|
|
|
// Always ensure we have two processed frames pending to allow for processing
|
|
|
|
// latency.
|
|
|
|
while (AudioQueue().GetSize() && (AudioQueue().IsFinished() ||
|
|
|
|
mProcessedQueueLength < LOW_AUDIO_USECS ||
|
|
|
|
mProcessedQueue.GetSize() < 2)) {
|
|
|
|
RefPtr<AudioData> data =
|
|
|
|
dont_AddRef(AudioQueue().PopFront().take()->As<AudioData>());
|
2016-04-21 17:32:21 +03:00
|
|
|
|
|
|
|
// Ignore the element with 0 frames and try next.
|
2016-04-13 10:17:54 +03:00
|
|
|
if (!data->mFrames) {
|
2016-04-21 17:32:21 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore invalid samples.
|
2016-04-13 10:17:54 +03:00
|
|
|
if (data->mRate != mConverter->InputConfig().Rate() ||
|
|
|
|
data->mChannels != mConverter->InputConfig().Channels()) {
|
2016-04-21 17:32:21 +03:00
|
|
|
NS_WARNING(nsPrintfCString(
|
|
|
|
"mismatched sample format, data=%p rate=%u channels=%u frames=%u",
|
2016-04-13 10:17:54 +03:00
|
|
|
data->mAudioData.get(), data->mRate, data->mChannels, data->mFrames).get());
|
2016-04-21 17:32:21 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if there's a gap in the audio. If there is, push silence into the
|
|
|
|
// audio hardware, so we can play across the gap.
|
|
|
|
// Calculate the timestamp of the next chunk of audio in numbers of
|
|
|
|
// samples.
|
2016-04-13 10:17:54 +03:00
|
|
|
CheckedInt64 sampleTime = UsecsToFrames(data->mTime - mStartTime,
|
|
|
|
data->mRate);
|
2016-04-21 17:32:21 +03:00
|
|
|
// Calculate the number of frames that have been pushed onto the audio hardware.
|
2016-04-13 10:17:54 +03:00
|
|
|
CheckedInt64 missingFrames = sampleTime - mFramesParsed;
|
2016-04-21 17:32:21 +03:00
|
|
|
|
2016-04-13 10:17:54 +03:00
|
|
|
if (!missingFrames.isValid()) {
|
2016-04-21 17:32:21 +03:00
|
|
|
NS_WARNING("Int overflow in DecodedAudioDataSink");
|
|
|
|
mErrored = true;
|
2016-04-13 10:17:54 +03:00
|
|
|
return;
|
2016-04-20 21:01:36 +03:00
|
|
|
}
|
|
|
|
|
2016-04-21 17:32:21 +03:00
|
|
|
if (missingFrames.value() > AUDIO_FUZZ_FRAMES) {
|
2016-04-13 10:17:54 +03:00
|
|
|
// The next audio packet begins some time after the end of the last packet
|
2016-04-21 17:32:21 +03:00
|
|
|
// we pushed to the audio hardware. We must push silence into the audio
|
2016-04-13 10:17:54 +03:00
|
|
|
// hardware so that the next audio packet begins playback at the correct
|
2016-04-21 17:32:21 +03:00
|
|
|
// time.
|
2016-04-13 10:17:54 +03:00
|
|
|
missingFrames = std::min<int64_t>(INT32_MAX, missingFrames.value());
|
|
|
|
mFramesParsed += missingFrames.value();
|
|
|
|
|
|
|
|
// We need to calculate how many frames are missing at the output rate.
|
|
|
|
missingFrames =
|
|
|
|
SaferMultDiv(missingFrames.value(), mOutputRate, data->mRate);
|
|
|
|
if (!missingFrames.isValid()) {
|
|
|
|
NS_WARNING("Int overflow in DecodedAudioDataSink");
|
|
|
|
mErrored = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (missingFrames.value()) {
|
|
|
|
AlignedAudioBuffer silenceData(missingFrames.value() * mOutputChannels);
|
|
|
|
if (!silenceData) {
|
|
|
|
NS_WARNING("OOM in DecodedAudioDataSink");
|
|
|
|
mErrored = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RefPtr<AudioData> silence = CreateAudioFromBuffer(Move(silenceData), data);
|
|
|
|
PushProcessedAudio(silence);
|
|
|
|
}
|
2016-04-21 17:32:21 +03:00
|
|
|
}
|
|
|
|
|
2016-04-13 10:17:54 +03:00
|
|
|
mLastEndTime = data->GetEndTime();
|
|
|
|
mFramesParsed += data->mFrames;
|
|
|
|
|
2016-04-21 17:32:21 +03:00
|
|
|
if (mConverter->InputConfig() != mConverter->OutputConfig()) {
|
|
|
|
AlignedAudioBuffer convertedData =
|
|
|
|
mConverter->Process(AudioSampleBuffer(Move(data->mAudioData))).Forget();
|
2016-04-13 10:17:54 +03:00
|
|
|
data = CreateAudioFromBuffer(Move(convertedData), data);
|
2016-04-21 17:32:21 +03:00
|
|
|
}
|
2016-04-13 10:17:54 +03:00
|
|
|
PushProcessedAudio(data);
|
2015-07-28 06:52:05 +03:00
|
|
|
}
|
|
|
|
|
2016-04-13 10:17:54 +03:00
|
|
|
if (AudioQueue().IsFinished()) {
|
|
|
|
mProcessedQueue.Finish();
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-13 10:17:54 +03:00
|
|
|
uint32_t
|
|
|
|
DecodedAudioDataSink::PushProcessedAudio(AudioData* aData)
|
2013-12-11 09:03:30 +04:00
|
|
|
{
|
2016-04-13 10:17:54 +03:00
|
|
|
if (!aData || !aData->mFrames) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
mProcessedQueue.Push(aData);
|
|
|
|
mProcessedQueueLength += FramesToUsecs(aData->mFrames, mOutputRate).value();
|
|
|
|
return aData->mFrames;
|
2013-12-11 09:03:30 +04:00
|
|
|
}
|
|
|
|
|
2016-04-13 10:17:54 +03:00
|
|
|
already_AddRefed<AudioData>
|
|
|
|
DecodedAudioDataSink::CreateAudioFromBuffer(AlignedAudioBuffer&& aBuffer,
|
|
|
|
AudioData* aReference)
|
2015-07-28 06:52:05 +03:00
|
|
|
{
|
2016-04-13 10:17:54 +03:00
|
|
|
uint32_t frames = aBuffer.Length() / mOutputChannels;
|
|
|
|
if (!frames) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
CheckedInt64 duration = FramesToUsecs(frames, mOutputRate);
|
|
|
|
if (!duration.isValid()) {
|
|
|
|
NS_WARNING("Int overflow in DecodedAudioDataSink");
|
|
|
|
mErrored = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
RefPtr<AudioData> data =
|
|
|
|
new AudioData(aReference->mOffset,
|
|
|
|
aReference->mTime,
|
|
|
|
duration.value(),
|
|
|
|
frames,
|
|
|
|
Move(aBuffer),
|
|
|
|
mOutputChannels,
|
|
|
|
mOutputRate);
|
|
|
|
return data.forget();
|
2015-07-28 06:52:05 +03:00
|
|
|
}
|
|
|
|
|
2015-08-18 06:55:01 +03:00
|
|
|
} // namespace media
|
2013-12-11 09:03:30 +04:00
|
|
|
} // namespace mozilla
|