2013-02-02 02:13:23 +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/. */
|
|
|
|
|
|
|
|
#include "MediaBufferDecoder.h"
|
2013-09-04 07:08:11 +04:00
|
|
|
#include "BufferDecoder.h"
|
2013-08-15 23:44:14 +04:00
|
|
|
#include "mozilla/dom/AudioContextBinding.h"
|
2014-05-09 22:51:56 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-02-02 02:13:23 +04:00
|
|
|
#include <speex/speex_resampler.h>
|
|
|
|
#include "nsXPCOMCIDInternal.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "MediaDecoderReader.h"
|
|
|
|
#include "BufferMediaResource.h"
|
|
|
|
#include "DecoderTraits.h"
|
|
|
|
#include "AudioContext.h"
|
|
|
|
#include "AudioBuffer.h"
|
2014-06-22 08:44:00 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2013-02-02 02:13:23 +04:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "nsMimeTypes.h"
|
2014-12-11 01:03:56 +03:00
|
|
|
#include "VideoUtils.h"
|
2013-10-01 10:22:57 +04:00
|
|
|
#include "WebAudioUtils.h"
|
2014-10-23 14:07:48 +04:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2013-02-02 02:13:23 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(WebAudioDecodeJob)
|
|
|
|
|
2013-07-16 13:00:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(WebAudioDecodeJob)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mContext)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mOutput)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSuccessCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFailureCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(WebAudioDecodeJob)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mContext)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOutput)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSuccessCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFailureCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(WebAudioDecodeJob)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(WebAudioDecodeJob, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(WebAudioDecodeJob, Release)
|
|
|
|
|
2013-02-12 01:56:57 +04:00
|
|
|
using namespace dom;
|
|
|
|
|
2015-04-28 09:42:00 +03:00
|
|
|
class ReportResultTask final : public nsRunnable
|
2013-02-02 02:13:23 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ReportResultTask(WebAudioDecodeJob& aDecodeJob,
|
|
|
|
WebAudioDecodeJob::ResultFn aFunction,
|
|
|
|
WebAudioDecodeJob::ErrorCode aErrorCode)
|
|
|
|
: mDecodeJob(aDecodeJob)
|
|
|
|
, mFunction(aFunction)
|
|
|
|
, mErrorCode(aErrorCode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
(mDecodeJob.*mFunction)(mErrorCode);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Note that the mDecodeJob member will probably die when mFunction is run.
|
|
|
|
// Therefore, it is not safe to do anything fancy with it in this class.
|
|
|
|
// Really, this class is only used because nsRunnableMethod doesn't support
|
|
|
|
// methods accepting arguments.
|
|
|
|
WebAudioDecodeJob& mDecodeJob;
|
|
|
|
WebAudioDecodeJob::ResultFn mFunction;
|
|
|
|
WebAudioDecodeJob::ErrorCode mErrorCode;
|
|
|
|
};
|
|
|
|
|
2015-04-28 09:42:00 +03:00
|
|
|
enum class PhaseEnum : int
|
|
|
|
{
|
2013-02-02 02:13:23 +04:00
|
|
|
Decode,
|
|
|
|
AllocateBuffer,
|
|
|
|
Done
|
2015-01-26 01:22:07 +03:00
|
|
|
};
|
2013-02-02 02:13:23 +04:00
|
|
|
|
2015-04-28 09:42:00 +03:00
|
|
|
class MediaDecodeTask final : public nsRunnable
|
2013-02-02 02:13:23 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
MediaDecodeTask(const char* aContentType, uint8_t* aBuffer,
|
|
|
|
uint32_t aLength,
|
2014-12-11 01:03:56 +03:00
|
|
|
WebAudioDecodeJob& aDecodeJob)
|
2013-02-02 02:13:23 +04:00
|
|
|
: mContentType(aContentType)
|
|
|
|
, mBuffer(aBuffer)
|
|
|
|
, mLength(aLength)
|
|
|
|
, mDecodeJob(aDecodeJob)
|
|
|
|
, mPhase(PhaseEnum::Decode)
|
2015-05-18 09:13:20 +03:00
|
|
|
, mFirstFrameDecoded(false)
|
2013-02-02 02:13:23 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aBuffer);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run();
|
|
|
|
bool CreateReader();
|
2014-12-11 01:03:56 +03:00
|
|
|
MediaDecoderReader* Reader() { MOZ_ASSERT(mDecoderReader); return mDecoderReader; }
|
2013-02-02 02:13:23 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
void ReportFailureOnMainThread(WebAudioDecodeJob::ErrorCode aErrorCode) {
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
Cleanup();
|
|
|
|
mDecodeJob.OnFailure(aErrorCode);
|
|
|
|
} else {
|
|
|
|
// Take extra care to cleanup on the main thread
|
2014-05-23 23:53:17 +04:00
|
|
|
NS_DispatchToMainThread(NS_NewRunnableMethod(this, &MediaDecodeTask::Cleanup));
|
2013-02-02 02:13:23 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
new ReportResultTask(mDecodeJob, &WebAudioDecodeJob::OnFailure, aErrorCode);
|
2014-05-23 23:53:17 +04:00
|
|
|
NS_DispatchToMainThread(event);
|
2013-02-02 02:13:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Decode();
|
2015-05-18 09:13:20 +03:00
|
|
|
void OnMetadataRead(MetadataHolder* aMetadata);
|
|
|
|
void OnMetadataNotRead(ReadMetadataFailureReason aReason);
|
2014-12-11 01:03:56 +03:00
|
|
|
void RequestSample();
|
|
|
|
void SampleDecoded(AudioData* aData);
|
|
|
|
void SampleNotDecoded(MediaDecoderReader::NotDecodedReason aReason);
|
|
|
|
void FinishDecode();
|
2013-02-02 02:13:23 +04:00
|
|
|
void AllocateBuffer();
|
|
|
|
void CallbackTheResult();
|
|
|
|
|
|
|
|
void Cleanup()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-03-10 01:27:15 +04:00
|
|
|
// MediaDecoderReader expects that BufferDecoder is alive.
|
|
|
|
// Destruct MediaDecoderReader first.
|
2014-11-25 04:09:19 +03:00
|
|
|
mDecoderReader->BreakCycles();
|
2013-02-02 02:13:23 +04:00
|
|
|
mDecoderReader = nullptr;
|
2014-03-10 01:27:15 +04:00
|
|
|
mBufferDecoder = nullptr;
|
2014-05-13 16:09:44 +04:00
|
|
|
JS_free(nullptr, mBuffer);
|
2013-02-02 02:13:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCString mContentType;
|
|
|
|
uint8_t* mBuffer;
|
|
|
|
uint32_t mLength;
|
|
|
|
WebAudioDecodeJob& mDecodeJob;
|
|
|
|
PhaseEnum mPhase;
|
|
|
|
nsRefPtr<BufferDecoder> mBufferDecoder;
|
2014-06-22 08:44:00 +04:00
|
|
|
nsRefPtr<MediaDecoderReader> mDecoderReader;
|
2014-12-11 01:03:56 +03:00
|
|
|
MediaInfo mMediaInfo;
|
|
|
|
MediaQueue<AudioData> mAudioQueue;
|
2015-05-18 09:13:20 +03:00
|
|
|
bool mFirstFrameDecoded;
|
2013-02-02 02:13:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MediaDecodeTask::Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mBufferDecoder);
|
|
|
|
MOZ_ASSERT(mDecoderReader);
|
|
|
|
switch (mPhase) {
|
|
|
|
case PhaseEnum::Decode:
|
|
|
|
Decode();
|
|
|
|
break;
|
|
|
|
case PhaseEnum::AllocateBuffer:
|
|
|
|
AllocateBuffer();
|
|
|
|
break;
|
|
|
|
case PhaseEnum::Done:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaDecodeTask::CreateReader()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-13 21:08:30 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(mDecodeJob.mContext->GetParentObject());
|
|
|
|
if (sop) {
|
|
|
|
principal = sop->GetPrincipal();
|
|
|
|
}
|
|
|
|
|
2013-04-16 04:08:03 +04:00
|
|
|
nsRefPtr<BufferMediaResource> resource =
|
2013-02-02 02:13:23 +04:00
|
|
|
new BufferMediaResource(static_cast<uint8_t*> (mBuffer),
|
2015-05-13 21:08:30 +03:00
|
|
|
mLength, principal, mContentType);
|
2013-02-02 02:13:23 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(!mBufferDecoder);
|
|
|
|
mBufferDecoder = new BufferDecoder(resource);
|
|
|
|
|
|
|
|
// If you change this list to add support for new decoders, please consider
|
2013-03-19 16:23:54 +04:00
|
|
|
// updating HTMLMediaElement::CreateDecoder as well.
|
2013-02-02 02:13:23 +04:00
|
|
|
|
2013-03-05 18:56:35 +04:00
|
|
|
mDecoderReader = DecoderTraits::CreateReader(mContentType, mBufferDecoder);
|
2013-02-02 02:13:23 +04:00
|
|
|
|
|
|
|
if (!mDecoderReader) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = mDecoderReader->Init(nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-28 09:42:00 +03:00
|
|
|
class AutoResampler final
|
|
|
|
{
|
2013-05-13 08:17:00 +04:00
|
|
|
public:
|
|
|
|
AutoResampler()
|
|
|
|
: mResampler(nullptr)
|
|
|
|
{}
|
|
|
|
~AutoResampler()
|
|
|
|
{
|
|
|
|
if (mResampler) {
|
|
|
|
speex_resampler_destroy(mResampler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
operator SpeexResamplerState*() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mResampler);
|
|
|
|
return mResampler;
|
|
|
|
}
|
|
|
|
void operator=(SpeexResamplerState* aResampler)
|
|
|
|
{
|
|
|
|
mResampler = aResampler;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SpeexResamplerState* mResampler;
|
|
|
|
};
|
|
|
|
|
2013-02-02 02:13:23 +04:00
|
|
|
void
|
|
|
|
MediaDecodeTask::Decode()
|
|
|
|
{
|
2014-04-23 16:56:42 +04:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2013-02-02 02:13:23 +04:00
|
|
|
|
2015-06-09 03:26:42 +03:00
|
|
|
mBufferDecoder->BeginDecoding(mDecoderReader->TaskQueue());
|
2013-02-02 02:13:23 +04:00
|
|
|
|
2013-08-29 13:43:44 +04:00
|
|
|
// Tell the decoder reader that we are not going to play the data directly,
|
|
|
|
// and that we should not reject files with more channels than the audio
|
2015-05-18 09:13:20 +03:00
|
|
|
// backend support.
|
2013-08-29 13:43:44 +04:00
|
|
|
mDecoderReader->SetIgnoreAudioOutputFormat();
|
|
|
|
|
2015-06-09 03:26:42 +03:00
|
|
|
mDecoderReader->AsyncReadMetadata()->Then(mDecoderReader->TaskQueue(), __func__, this,
|
2015-05-18 09:13:20 +03:00
|
|
|
&MediaDecodeTask::OnMetadataRead,
|
|
|
|
&MediaDecodeTask::OnMetadataNotRead);
|
|
|
|
}
|
2013-02-02 02:13:23 +04:00
|
|
|
|
2015-05-18 09:13:20 +03:00
|
|
|
void
|
|
|
|
MediaDecodeTask::OnMetadataRead(MetadataHolder* aMetadata)
|
|
|
|
{
|
|
|
|
mMediaInfo = aMetadata->mInfo;
|
|
|
|
if (!mMediaInfo.HasAudio()) {
|
2014-11-25 04:09:19 +03:00
|
|
|
mDecoderReader->Shutdown();
|
2013-02-02 02:13:23 +04:00
|
|
|
ReportFailureOnMainThread(WebAudioDecodeJob::NoAudio);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-11 01:03:56 +03:00
|
|
|
RequestSample();
|
|
|
|
}
|
|
|
|
|
2015-05-18 09:13:20 +03:00
|
|
|
void
|
|
|
|
MediaDecodeTask::OnMetadataNotRead(ReadMetadataFailureReason aReason)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(aReason != ReadMetadataFailureReason::WAITING_FOR_RESOURCES);
|
|
|
|
mDecoderReader->Shutdown();
|
|
|
|
ReportFailureOnMainThread(WebAudioDecodeJob::InvalidContent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-11 01:03:56 +03:00
|
|
|
void
|
|
|
|
MediaDecodeTask::RequestSample()
|
|
|
|
{
|
2015-06-09 03:26:42 +03:00
|
|
|
mDecoderReader->RequestAudioData()->Then(mDecoderReader->TaskQueue(), __func__, this,
|
2014-12-11 01:03:56 +03:00
|
|
|
&MediaDecodeTask::SampleDecoded,
|
|
|
|
&MediaDecodeTask::SampleNotDecoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecodeTask::SampleDecoded(AudioData* aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
mAudioQueue.Push(aData);
|
2015-05-18 09:13:20 +03:00
|
|
|
if (!mFirstFrameDecoded) {
|
|
|
|
mDecoderReader->ReadUpdatedMetadata(&mMediaInfo);
|
|
|
|
mFirstFrameDecoded = true;
|
|
|
|
}
|
2014-12-11 01:03:56 +03:00
|
|
|
RequestSample();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecodeTask::SampleNotDecoded(MediaDecoderReader::NotDecodedReason aReason)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
if (aReason == MediaDecoderReader::DECODE_ERROR) {
|
|
|
|
mDecoderReader->Shutdown();
|
|
|
|
ReportFailureOnMainThread(WebAudioDecodeJob::InvalidContent);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aReason == MediaDecoderReader::END_OF_STREAM);
|
|
|
|
FinishDecode();
|
2013-02-02 02:13:23 +04:00
|
|
|
}
|
2014-12-11 01:03:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecodeTask::FinishDecode()
|
|
|
|
{
|
2014-06-18 09:07:02 +04:00
|
|
|
mDecoderReader->Shutdown();
|
2013-02-02 02:13:23 +04:00
|
|
|
|
2014-12-11 01:03:56 +03:00
|
|
|
uint32_t frameCount = mAudioQueue.FrameCount();
|
|
|
|
uint32_t channelCount = mMediaInfo.mAudio.mChannels;
|
|
|
|
uint32_t sampleRate = mMediaInfo.mAudio.mRate;
|
2013-02-02 02:13:23 +04:00
|
|
|
|
|
|
|
if (!frameCount || !channelCount || !sampleRate) {
|
|
|
|
ReportFailureOnMainThread(WebAudioDecodeJob::InvalidContent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t destSampleRate = mDecodeJob.mContext->SampleRate();
|
2013-05-13 08:17:00 +04:00
|
|
|
AutoResampler resampler;
|
2013-02-02 02:13:23 +04:00
|
|
|
|
2013-05-13 08:17:00 +04:00
|
|
|
uint32_t resampledFrames = frameCount;
|
|
|
|
if (sampleRate != destSampleRate) {
|
|
|
|
resampledFrames = static_cast<uint32_t>(
|
2013-02-02 02:13:23 +04:00
|
|
|
static_cast<uint64_t>(destSampleRate) *
|
|
|
|
static_cast<uint64_t>(frameCount) /
|
2013-05-13 08:17:00 +04:00
|
|
|
static_cast<uint64_t>(sampleRate)
|
2013-02-02 02:13:23 +04:00
|
|
|
);
|
|
|
|
|
2013-05-13 08:17:00 +04:00
|
|
|
resampler = speex_resampler_init(channelCount,
|
|
|
|
sampleRate,
|
2013-02-02 02:13:23 +04:00
|
|
|
destSampleRate,
|
|
|
|
SPEEX_RESAMPLER_QUALITY_DEFAULT, nullptr);
|
2013-05-13 08:17:00 +04:00
|
|
|
speex_resampler_skip_zeros(resampler);
|
|
|
|
resampledFrames += speex_resampler_get_output_latency(resampler);
|
2013-02-02 02:13:23 +04:00
|
|
|
}
|
|
|
|
|
2013-05-13 08:17:00 +04:00
|
|
|
// Allocate the channel buffers. Note that if we end up resampling, we may
|
|
|
|
// write fewer bytes than mResampledFrames to the output buffer, in which
|
|
|
|
// case mWriteIndex will tell us how many valid samples we have.
|
|
|
|
bool memoryAllocationSuccess = true;
|
2015-05-18 23:50:35 +03:00
|
|
|
if (!mDecodeJob.mChannelBuffers.SetLength(channelCount, fallible)) {
|
2013-05-13 08:17:00 +04:00
|
|
|
memoryAllocationSuccess = false;
|
|
|
|
} else {
|
|
|
|
for (uint32_t i = 0; i < channelCount; ++i) {
|
2015-01-28 12:00:40 +03:00
|
|
|
mDecodeJob.mChannelBuffers[i] = new (fallible) float[resampledFrames];
|
2013-05-13 08:17:00 +04:00
|
|
|
if (!mDecodeJob.mChannelBuffers[i]) {
|
|
|
|
memoryAllocationSuccess = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!memoryAllocationSuccess) {
|
|
|
|
ReportFailureOnMainThread(WebAudioDecodeJob::UnknownError);
|
|
|
|
return;
|
|
|
|
}
|
2013-02-02 02:13:23 +04:00
|
|
|
|
2014-11-20 00:01:10 +03:00
|
|
|
nsRefPtr<AudioData> audioData;
|
2014-12-11 01:03:56 +03:00
|
|
|
while ((audioData = mAudioQueue.PopFront())) {
|
2013-02-02 02:13:23 +04:00
|
|
|
audioData->EnsureAudioBuffer(); // could lead to a copy :(
|
|
|
|
AudioDataValue* bufferData = static_cast<AudioDataValue*>
|
|
|
|
(audioData->mAudioBuffer->Data());
|
|
|
|
|
2013-05-13 08:17:00 +04:00
|
|
|
if (sampleRate != destSampleRate) {
|
2013-12-03 03:09:01 +04:00
|
|
|
const uint32_t maxOutSamples = resampledFrames - mDecodeJob.mWriteIndex;
|
2013-02-02 02:13:23 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < audioData->mChannels; ++i) {
|
|
|
|
uint32_t inSamples = audioData->mFrames;
|
2013-12-03 03:09:01 +04:00
|
|
|
uint32_t outSamples = maxOutSamples;
|
2013-02-02 02:13:23 +04:00
|
|
|
|
2013-10-01 10:22:57 +04:00
|
|
|
WebAudioUtils::SpeexResamplerProcess(
|
|
|
|
resampler, i, &bufferData[i * audioData->mFrames], &inSamples,
|
|
|
|
mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
|
|
|
|
&outSamples);
|
2013-05-13 08:17:00 +04:00
|
|
|
|
|
|
|
if (i == audioData->mChannels - 1) {
|
|
|
|
mDecodeJob.mWriteIndex += outSamples;
|
|
|
|
MOZ_ASSERT(mDecodeJob.mWriteIndex <= resampledFrames);
|
2013-12-03 03:09:01 +04:00
|
|
|
MOZ_ASSERT(inSamples == audioData->mFrames);
|
2013-05-13 08:17:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (uint32_t i = 0; i < audioData->mChannels; ++i) {
|
|
|
|
ConvertAudioSamples(&bufferData[i * audioData->mFrames],
|
|
|
|
mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
|
|
|
|
audioData->mFrames);
|
|
|
|
|
|
|
|
if (i == audioData->mChannels - 1) {
|
|
|
|
mDecodeJob.mWriteIndex += audioData->mFrames;
|
|
|
|
}
|
2013-02-02 02:13:23 +04:00
|
|
|
}
|
|
|
|
}
|
2013-05-13 08:17:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sampleRate != destSampleRate) {
|
2013-12-03 03:09:01 +04:00
|
|
|
uint32_t inputLatency = speex_resampler_get_input_latency(resampler);
|
|
|
|
const uint32_t maxOutSamples = resampledFrames - mDecodeJob.mWriteIndex;
|
2013-05-13 08:17:00 +04:00
|
|
|
for (uint32_t i = 0; i < channelCount; ++i) {
|
|
|
|
uint32_t inSamples = inputLatency;
|
2013-12-03 03:09:01 +04:00
|
|
|
uint32_t outSamples = maxOutSamples;
|
2013-05-13 08:17:00 +04:00
|
|
|
|
2013-10-01 10:22:57 +04:00
|
|
|
WebAudioUtils::SpeexResamplerProcess(
|
2013-12-03 03:07:17 +04:00
|
|
|
resampler, i, (AudioDataValue*)nullptr, &inSamples,
|
2013-10-01 10:22:57 +04:00
|
|
|
mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
|
|
|
|
&outSamples);
|
2013-05-13 08:17:00 +04:00
|
|
|
|
|
|
|
if (i == channelCount - 1) {
|
|
|
|
mDecodeJob.mWriteIndex += outSamples;
|
|
|
|
MOZ_ASSERT(mDecodeJob.mWriteIndex <= resampledFrames);
|
2013-12-03 03:09:01 +04:00
|
|
|
MOZ_ASSERT(inSamples == inputLatency);
|
2013-05-13 08:17:00 +04:00
|
|
|
}
|
2013-02-02 02:13:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-13 08:17:00 +04:00
|
|
|
mPhase = PhaseEnum::AllocateBuffer;
|
2014-04-23 16:56:42 +04:00
|
|
|
NS_DispatchToMainThread(this);
|
2013-02-02 02:13:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-05-13 08:17:00 +04:00
|
|
|
MediaDecodeTask::AllocateBuffer()
|
2013-02-02 02:13:23 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-05-13 08:17:00 +04:00
|
|
|
if (!mDecodeJob.AllocateBuffer()) {
|
2013-02-02 02:13:23 +04:00
|
|
|
ReportFailureOnMainThread(WebAudioDecodeJob::UnknownError);
|
2013-05-13 08:17:00 +04:00
|
|
|
return;
|
2013-02-02 02:13:23 +04:00
|
|
|
}
|
|
|
|
|
2013-05-13 08:17:00 +04:00
|
|
|
mPhase = PhaseEnum::Done;
|
|
|
|
CallbackTheResult();
|
2013-02-02 02:13:23 +04:00
|
|
|
}
|
|
|
|
|
2013-05-13 08:17:00 +04:00
|
|
|
void
|
|
|
|
MediaDecodeTask::CallbackTheResult()
|
2013-02-02 02:13:23 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-05-13 08:17:00 +04:00
|
|
|
Cleanup();
|
2013-02-02 02:13:23 +04:00
|
|
|
|
2013-05-13 08:17:00 +04:00
|
|
|
// Now, we're ready to call the script back with the resulting buffer
|
|
|
|
mDecodeJob.OnSuccess(WebAudioDecodeJob::NoError);
|
2013-02-02 02:13:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WebAudioDecodeJob::AllocateBuffer()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mOutput);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2014-06-19 11:21:14 +04:00
|
|
|
AutoJSAPI jsapi;
|
2014-06-25 14:17:17 +04:00
|
|
|
if (NS_WARN_IF(!jsapi.Init(mContext->GetOwner()))) {
|
2013-02-02 02:13:23 +04:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-09 22:51:56 +04:00
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
|
2013-02-02 02:13:23 +04:00
|
|
|
// Now create the AudioBuffer
|
2014-05-16 01:23:27 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
mOutput = AudioBuffer::Create(mContext, mChannelBuffers.Length(),
|
|
|
|
mWriteIndex, mContext->SampleRate(), cx, rv);
|
|
|
|
if (rv.Failed()) {
|
2013-02-02 02:13:23 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-13 08:17:00 +04:00
|
|
|
for (uint32_t i = 0; i < mChannelBuffers.Length(); ++i) {
|
2014-05-09 13:16:03 +04:00
|
|
|
mOutput->SetRawChannelContents(i, mChannelBuffers[i]);
|
2013-02-02 02:13:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-11 01:03:56 +03:00
|
|
|
AsyncDecodeWebAudio(const char* aContentType, uint8_t* aBuffer,
|
|
|
|
uint32_t aLength, WebAudioDecodeJob& aDecodeJob)
|
2013-02-02 02:13:23 +04:00
|
|
|
{
|
|
|
|
// Do not attempt to decode the media if we were not successful at sniffing
|
|
|
|
// the content type.
|
|
|
|
if (!*aContentType ||
|
|
|
|
strcmp(aContentType, APPLICATION_OCTET_STREAM) == 0) {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
new ReportResultTask(aDecodeJob,
|
|
|
|
&WebAudioDecodeJob::OnFailure,
|
|
|
|
WebAudioDecodeJob::UnknownContent);
|
2014-06-22 22:10:08 +04:00
|
|
|
JS_free(nullptr, aBuffer);
|
2014-05-23 23:53:17 +04:00
|
|
|
NS_DispatchToMainThread(event);
|
2013-02-02 02:13:23 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-11 01:03:56 +03:00
|
|
|
RefPtr<MediaDecodeTask> task =
|
|
|
|
new MediaDecodeTask(aContentType, aBuffer, aLength, aDecodeJob);
|
2013-02-02 02:13:23 +04:00
|
|
|
if (!task->CreateReader()) {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
new ReportResultTask(aDecodeJob,
|
|
|
|
&WebAudioDecodeJob::OnFailure,
|
|
|
|
WebAudioDecodeJob::UnknownError);
|
2014-05-23 23:53:17 +04:00
|
|
|
NS_DispatchToMainThread(event);
|
2013-02-02 02:13:23 +04:00
|
|
|
} else {
|
2015-05-01 16:14:16 +03:00
|
|
|
// If we did this without a temporary:
|
2015-06-09 03:26:42 +03:00
|
|
|
// task->Reader()->TaskQueue()->Dispatch(task.forget())
|
2015-05-01 16:14:16 +03:00
|
|
|
// we might evaluate the task.forget() before calling Reader(). Enforce
|
|
|
|
// a non-crashy order-of-operations.
|
2015-06-09 03:26:42 +03:00
|
|
|
MediaTaskQueue* taskQueue = task->Reader()->TaskQueue();
|
2015-05-01 16:14:16 +03:00
|
|
|
taskQueue->Dispatch(task.forget());
|
2014-06-18 09:24:05 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-02 02:13:23 +04:00
|
|
|
WebAudioDecodeJob::WebAudioDecodeJob(const nsACString& aContentType,
|
|
|
|
AudioContext* aContext,
|
2014-10-23 14:07:48 +04:00
|
|
|
Promise* aPromise,
|
2013-02-02 02:13:23 +04:00
|
|
|
DecodeSuccessCallback* aSuccessCallback,
|
|
|
|
DecodeErrorCallback* aFailureCallback)
|
|
|
|
: mContentType(aContentType)
|
2013-05-13 08:17:00 +04:00
|
|
|
, mWriteIndex(0)
|
2013-02-02 02:13:23 +04:00
|
|
|
, mContext(aContext)
|
2014-10-23 14:07:48 +04:00
|
|
|
, mPromise(aPromise)
|
2013-02-02 02:13:23 +04:00
|
|
|
, mSuccessCallback(aSuccessCallback)
|
|
|
|
, mFailureCallback(aFailureCallback)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aContext);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_COUNT_CTOR(WebAudioDecodeJob);
|
|
|
|
}
|
|
|
|
|
|
|
|
WebAudioDecodeJob::~WebAudioDecodeJob()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_COUNT_DTOR(WebAudioDecodeJob);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebAudioDecodeJob::OnSuccess(ErrorCode aErrorCode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aErrorCode == NoError);
|
|
|
|
|
|
|
|
// Ignore errors in calling the callback, since there is not much that we can
|
|
|
|
// do about it here.
|
2014-04-23 16:56:42 +04:00
|
|
|
ErrorResult rv;
|
2014-10-23 14:07:48 +04:00
|
|
|
if (mSuccessCallback) {
|
|
|
|
mSuccessCallback->Call(*mOutput, rv);
|
|
|
|
}
|
|
|
|
mPromise->MaybeResolve(mOutput);
|
2013-02-02 02:13:23 +04:00
|
|
|
|
|
|
|
mContext->RemoveFromDecodeQueue(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebAudioDecodeJob::OnFailure(ErrorCode aErrorCode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
const char* errorMessage;
|
|
|
|
switch (aErrorCode) {
|
|
|
|
case NoError:
|
|
|
|
MOZ_ASSERT(false, "Who passed NoError to OnFailure?");
|
|
|
|
// Fall through to get some sort of a sane error message if this actually
|
|
|
|
// happens at runtime.
|
|
|
|
case UnknownError:
|
|
|
|
errorMessage = "MediaDecodeAudioDataUnknownError";
|
|
|
|
break;
|
|
|
|
case UnknownContent:
|
|
|
|
errorMessage = "MediaDecodeAudioDataUnknownContentType";
|
|
|
|
break;
|
|
|
|
case InvalidContent:
|
|
|
|
errorMessage = "MediaDecodeAudioDataInvalidContent";
|
|
|
|
break;
|
|
|
|
case NoAudio:
|
|
|
|
errorMessage = "MediaDecodeAudioDataNoAudio";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> pWindow = do_QueryInterface(mContext->GetParentObject());
|
|
|
|
nsIDocument* doc = nullptr;
|
|
|
|
if (pWindow) {
|
|
|
|
doc = pWindow->GetExtantDoc();
|
|
|
|
}
|
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::errorFlag,
|
2013-08-21 23:28:26 +04:00
|
|
|
NS_LITERAL_CSTRING("Media"),
|
2013-02-02 02:13:23 +04:00
|
|
|
doc,
|
|
|
|
nsContentUtils::eDOM_PROPERTIES,
|
|
|
|
errorMessage);
|
|
|
|
|
|
|
|
// Ignore errors in calling the callback, since there is not much that we can
|
|
|
|
// do about it here.
|
2013-03-05 08:04:51 +04:00
|
|
|
if (mFailureCallback) {
|
|
|
|
ErrorResult rv;
|
|
|
|
mFailureCallback->Call(rv);
|
|
|
|
}
|
2013-02-02 02:13:23 +04:00
|
|
|
|
2014-10-23 14:07:48 +04:00
|
|
|
mPromise->MaybeReject(NS_ERROR_DOM_ENCODING_NOT_SUPPORTED_ERR);
|
|
|
|
|
2013-02-02 02:13:23 +04:00
|
|
|
mContext->RemoveFromDecodeQueue(this);
|
|
|
|
}
|
|
|
|
|
2014-01-04 22:15:41 +04:00
|
|
|
size_t
|
|
|
|
WebAudioDecodeJob::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = 0;
|
|
|
|
amount += mContentType.SizeOfExcludingThisMustBeUnshared(aMallocSizeOf);
|
|
|
|
if (mSuccessCallback) {
|
|
|
|
amount += mSuccessCallback->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
if (mFailureCallback) {
|
|
|
|
amount += mFailureCallback->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
if (mOutput) {
|
|
|
|
amount += mOutput->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
amount += mChannelBuffers.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
for (uint32_t i = 0; i < mChannelBuffers.Length(); ++i) {
|
|
|
|
amount += mChannelBuffers[i].SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2014-05-01 21:37:54 +04:00
|
|
|
size_t
|
|
|
|
WebAudioDecodeJob::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|
2013-02-02 02:13:23 +04:00
|
|
|
|