2010-08-25 06:10:00 -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/. */
|
2010-08-25 06:10:00 -07:00
|
|
|
|
2013-09-05 10:29:38 -07:00
|
|
|
#include "nsTArray.h"
|
2012-11-14 11:46:40 -08:00
|
|
|
#include "AudioAvailableEventManager.h"
|
2011-04-13 15:12:23 -07:00
|
|
|
#include "VideoUtils.h"
|
2013-09-05 10:29:38 -07:00
|
|
|
|
2010-08-25 06:10:00 -07:00
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
static const nsTArray< nsCOMPtr<nsIRunnable> >::size_type MAX_PENDING_EVENTS = 100;
|
2010-08-25 06:10:00 -07:00
|
|
|
|
|
|
|
class nsAudioAvailableEventRunner : public nsRunnable
|
|
|
|
{
|
|
|
|
private:
|
2012-11-19 07:11:21 -08:00
|
|
|
nsRefPtr<MediaDecoder> mDecoder;
|
2010-08-25 06:10:00 -07:00
|
|
|
nsAutoArrayPtr<float> mFrameBuffer;
|
|
|
|
public:
|
2012-11-14 11:46:40 -08:00
|
|
|
nsAudioAvailableEventRunner(MediaDecoder* aDecoder, float* aFrameBuffer,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFrameBufferLength, float aTime) :
|
2010-08-25 06:10:00 -07:00
|
|
|
mDecoder(aDecoder),
|
|
|
|
mFrameBuffer(aFrameBuffer),
|
|
|
|
mFrameBufferLength(aFrameBufferLength),
|
|
|
|
mTime(aTime)
|
|
|
|
{
|
2010-09-05 19:14:50 -07:00
|
|
|
MOZ_COUNT_CTOR(nsAudioAvailableEventRunner);
|
|
|
|
}
|
|
|
|
|
|
|
|
~nsAudioAvailableEventRunner() {
|
|
|
|
MOZ_COUNT_DTOR(nsAudioAvailableEventRunner);
|
2010-08-25 06:10:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
mDecoder->AudioAvailable(mFrameBuffer.forget(), mFrameBufferLength, mTime);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint32_t mFrameBufferLength;
|
2010-10-06 15:58:36 -07:00
|
|
|
|
|
|
|
// Start time of the buffer data (in seconds).
|
|
|
|
const float mTime;
|
2010-08-25 06:10:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
AudioAvailableEventManager::AudioAvailableEventManager(MediaDecoder* aDecoder) :
|
2010-08-25 06:10:00 -07:00
|
|
|
mDecoder(aDecoder),
|
|
|
|
mSignalBuffer(new float[mDecoder->GetFrameBufferLength()]),
|
|
|
|
mSignalBufferLength(mDecoder->GetFrameBufferLength()),
|
2011-04-11 14:15:45 -07:00
|
|
|
mNewSignalBufferLength(mSignalBufferLength),
|
2010-08-25 06:10:00 -07:00
|
|
|
mSignalBufferPosition(0),
|
2011-11-21 16:34:21 -08:00
|
|
|
mReentrantMonitor("media.audioavailableeventmanager"),
|
|
|
|
mHasListener(false)
|
2010-08-25 06:10:00 -07:00
|
|
|
{
|
2012-11-14 11:46:40 -08:00
|
|
|
MOZ_COUNT_CTOR(AudioAvailableEventManager);
|
2010-09-05 19:14:50 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
AudioAvailableEventManager::~AudioAvailableEventManager()
|
2010-09-05 19:14:50 -07:00
|
|
|
{
|
2012-11-14 11:46:40 -08:00
|
|
|
MOZ_COUNT_DTOR(AudioAvailableEventManager);
|
2010-08-25 06:10:00 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
void AudioAvailableEventManager::Init(uint32_t aChannels, uint32_t aRate)
|
2010-08-25 06:10:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aChannels != 0 && aRate != 0, "Audio metadata not known.");
|
2011-03-23 15:28:58 -07:00
|
|
|
mSamplesPerSecond = static_cast<float>(aChannels * aRate);
|
2010-08-25 06:10:00 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
void AudioAvailableEventManager::DispatchPendingEvents(uint64_t aCurrentTime)
|
2010-08-25 06:10:00 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-08-25 06:10:00 -07:00
|
|
|
|
2011-11-21 16:34:21 -08:00
|
|
|
if (!mHasListener) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-25 06:10:00 -07:00
|
|
|
while (mPendingEvents.Length() > 0) {
|
|
|
|
nsAudioAvailableEventRunner* e =
|
|
|
|
(nsAudioAvailableEventRunner*)mPendingEvents[0].get();
|
2011-04-13 15:12:23 -07:00
|
|
|
if (e->mTime * USECS_PER_S > aCurrentTime) {
|
2010-08-25 06:10:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIRunnable> event = mPendingEvents[0];
|
|
|
|
mPendingEvents.RemoveElementAt(0);
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
void AudioAvailableEventManager::QueueWrittenAudioData(AudioDataValue* aAudioData,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aAudioDataLength,
|
|
|
|
uint64_t aEndTimeSampleOffset)
|
2010-08-25 06:10:00 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2011-04-11 14:15:45 -07:00
|
|
|
|
2011-11-21 16:34:21 -08:00
|
|
|
if (!mHasListener) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t currentBufferSize = mNewSignalBufferLength;
|
2010-08-25 06:10:00 -07:00
|
|
|
if (currentBufferSize == 0) {
|
|
|
|
NS_WARNING("Decoder framebuffer length not set.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mSignalBuffer ||
|
|
|
|
(mSignalBufferPosition == 0 && mSignalBufferLength != currentBufferSize)) {
|
|
|
|
if (!mSignalBuffer || (mSignalBufferLength < currentBufferSize)) {
|
|
|
|
// Only resize if buffer is empty or smaller.
|
|
|
|
mSignalBuffer = new float[currentBufferSize];
|
|
|
|
}
|
|
|
|
mSignalBufferLength = currentBufferSize;
|
|
|
|
}
|
2011-08-15 22:19:51 -07:00
|
|
|
AudioDataValue* audioData = aAudioData;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t audioDataLength = aAudioDataLength;
|
|
|
|
uint32_t signalBufferTail = mSignalBufferLength - mSignalBufferPosition;
|
2010-08-25 06:10:00 -07:00
|
|
|
|
|
|
|
// Group audio samples into optimal size for event dispatch, and queue.
|
|
|
|
while (signalBufferTail <= audioDataLength) {
|
2010-10-06 15:58:36 -07:00
|
|
|
float time = 0.0;
|
2010-08-25 06:10:00 -07:00
|
|
|
// Guard against unsigned number overflow during first frame time calculation.
|
|
|
|
if (aEndTimeSampleOffset > mSignalBufferPosition + audioDataLength) {
|
2010-10-06 15:58:36 -07:00
|
|
|
time = (aEndTimeSampleOffset - mSignalBufferPosition - audioDataLength) /
|
|
|
|
mSamplesPerSecond;
|
2010-08-25 06:10:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fill the signalBuffer.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2010-10-18 19:55:45 -07:00
|
|
|
float *signalBuffer = mSignalBuffer.get() + mSignalBufferPosition;
|
2012-04-29 20:12:42 -07:00
|
|
|
if (audioData) {
|
|
|
|
for (i = 0; i < signalBufferTail; ++i) {
|
2012-10-25 03:09:40 -07:00
|
|
|
signalBuffer[i] = AudioSampleToFloat(audioData[i]);
|
2012-04-29 20:12:42 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
memset(signalBuffer, 0, signalBufferTail*sizeof(signalBuffer[0]));
|
|
|
|
}
|
|
|
|
if (audioData) {
|
|
|
|
audioData += signalBufferTail;
|
2010-10-18 19:55:45 -07:00
|
|
|
}
|
2012-04-16 14:29:14 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(audioDataLength >= signalBufferTail,
|
|
|
|
"audioDataLength about to wrap past zero to +infinity!");
|
2010-08-25 06:10:00 -07:00
|
|
|
audioDataLength -= signalBufferTail;
|
|
|
|
|
|
|
|
if (mPendingEvents.Length() > 0) {
|
|
|
|
// Check last event timecode to make sure that all queued events
|
|
|
|
// are in non-descending sequence.
|
|
|
|
nsAudioAvailableEventRunner* lastPendingEvent =
|
|
|
|
(nsAudioAvailableEventRunner*)mPendingEvents[mPendingEvents.Length() - 1].get();
|
|
|
|
if (lastPendingEvent->mTime > time) {
|
|
|
|
// Clear the queue to start a fresh sequence.
|
|
|
|
mPendingEvents.Clear();
|
|
|
|
} else if (mPendingEvents.Length() >= MAX_PENDING_EVENTS) {
|
|
|
|
NS_WARNING("Hit audio event queue max.");
|
|
|
|
mPendingEvents.RemoveElementsAt(0, mPendingEvents.Length() - MAX_PENDING_EVENTS + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-15 22:19:51 -07:00
|
|
|
// Inform the element that we've written audio data.
|
2010-08-25 06:10:00 -07:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
new nsAudioAvailableEventRunner(mDecoder, mSignalBuffer.forget(),
|
|
|
|
mSignalBufferLength, time);
|
|
|
|
mPendingEvents.AppendElement(event);
|
|
|
|
|
|
|
|
// Reset the buffer
|
|
|
|
mSignalBufferLength = currentBufferSize;
|
|
|
|
mSignalBuffer = new float[currentBufferSize];
|
|
|
|
mSignalBufferPosition = 0;
|
|
|
|
signalBufferTail = currentBufferSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mSignalBufferPosition + audioDataLength < mSignalBufferLength,
|
|
|
|
"Intermediate signal buffer must fit at least one more item.");
|
|
|
|
|
|
|
|
if (audioDataLength > 0) {
|
|
|
|
// Add data to the signalBuffer.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2010-10-18 19:55:45 -07:00
|
|
|
float *signalBuffer = mSignalBuffer.get() + mSignalBufferPosition;
|
2012-04-29 20:12:42 -07:00
|
|
|
if (audioData) {
|
|
|
|
for (i = 0; i < audioDataLength; ++i) {
|
2012-10-25 03:09:40 -07:00
|
|
|
signalBuffer[i] = AudioSampleToFloat(audioData[i]);
|
2012-04-29 20:12:42 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
memset(signalBuffer, 0, audioDataLength*sizeof(signalBuffer[0]));
|
2010-10-18 19:55:45 -07:00
|
|
|
}
|
2010-08-25 06:10:00 -07:00
|
|
|
mSignalBufferPosition += audioDataLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
void AudioAvailableEventManager::Clear()
|
2010-08-25 06:10:00 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-08-25 06:10:00 -07:00
|
|
|
|
|
|
|
mPendingEvents.Clear();
|
|
|
|
mSignalBufferPosition = 0;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
void AudioAvailableEventManager::Drain(uint64_t aEndTime)
|
2010-08-25 06:10:00 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-08-25 06:10:00 -07:00
|
|
|
|
2011-11-21 16:34:21 -08:00
|
|
|
if (!mHasListener) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-25 06:10:00 -07:00
|
|
|
// Force all pending events to go now.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mPendingEvents.Length(); ++i) {
|
2010-08-25 06:10:00 -07:00
|
|
|
nsCOMPtr<nsIRunnable> event = mPendingEvents[i];
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
mPendingEvents.Clear();
|
|
|
|
|
|
|
|
// If there is anything left in the signal buffer, put it in an event and fire.
|
|
|
|
if (0 == mSignalBufferPosition)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Zero-pad the end of the signal buffer so it's complete.
|
|
|
|
memset(mSignalBuffer.get() + mSignalBufferPosition, 0,
|
|
|
|
(mSignalBufferLength - mSignalBufferPosition) * sizeof(float));
|
|
|
|
|
|
|
|
// Force this last event to go now.
|
2011-04-13 15:12:23 -07:00
|
|
|
float time = (aEndTime / static_cast<float>(USECS_PER_S)) -
|
2010-10-06 15:58:36 -07:00
|
|
|
(mSignalBufferPosition / mSamplesPerSecond);
|
2010-08-25 06:10:00 -07:00
|
|
|
nsCOMPtr<nsIRunnable> lastEvent =
|
|
|
|
new nsAudioAvailableEventRunner(mDecoder, mSignalBuffer.forget(),
|
2010-10-06 15:58:36 -07:00
|
|
|
mSignalBufferLength, time);
|
2010-08-25 06:10:00 -07:00
|
|
|
NS_DispatchToMainThread(lastEvent, NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
mSignalBufferPosition = 0;
|
|
|
|
}
|
2011-04-11 14:15:45 -07:00
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
void AudioAvailableEventManager::SetSignalBufferLength(uint32_t aLength)
|
2011-04-11 14:15:45 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2011-04-11 14:15:45 -07:00
|
|
|
|
|
|
|
mNewSignalBufferLength = aLength;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
void AudioAvailableEventManager::NotifyAudioAvailableListener()
|
2011-11-21 16:34:21 -08:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
|
|
|
|
mHasListener = true;
|
|
|
|
}
|
2012-11-14 11:45:33 -08:00
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|