This source file includes following definitions.
- m_hasEndedListener
- updateSchedulingInfo
- start
- stop
- setOnended
- finish
- notifyEndedDispatch
- notifyEnded
#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "modules/webaudio/AudioScheduledSourceNode.h"
#include "bindings/v8/ExceptionState.h"
#include "core/dom/ExceptionCode.h"
#include "core/events/Event.h"
#include "platform/audio/AudioUtilities.h"
#include "modules/webaudio/AudioContext.h"
#include <algorithm>
#include "wtf/MathExtras.h"
using namespace std;
namespace WebCore {
const double AudioScheduledSourceNode::UnknownTime = -1;
AudioScheduledSourceNode::AudioScheduledSourceNode(AudioContext* context, float sampleRate)
: AudioSourceNode(context, sampleRate)
, m_playbackState(UNSCHEDULED_STATE)
, m_startTime(0)
, m_endTime(UnknownTime)
, m_hasEndedListener(false)
{
}
void AudioScheduledSourceNode::updateSchedulingInfo(size_t quantumFrameSize,
AudioBus* outputBus,
size_t& quantumFrameOffset,
size_t& nonSilentFramesToProcess)
{
ASSERT(outputBus);
if (!outputBus)
return;
ASSERT(quantumFrameSize == AudioNode::ProcessingSizeInFrames);
if (quantumFrameSize != AudioNode::ProcessingSizeInFrames)
return;
double sampleRate = this->sampleRate();
size_t quantumStartFrame = context()->currentSampleFrame();
size_t quantumEndFrame = quantumStartFrame + quantumFrameSize;
size_t startFrame = AudioUtilities::timeToSampleFrame(m_startTime, sampleRate);
size_t endFrame = m_endTime == UnknownTime ? 0 : AudioUtilities::timeToSampleFrame(m_endTime, sampleRate);
if (m_endTime != UnknownTime && endFrame <= quantumStartFrame)
finish();
if (m_playbackState == UNSCHEDULED_STATE || m_playbackState == FINISHED_STATE || startFrame >= quantumEndFrame) {
outputBus->zero();
nonSilentFramesToProcess = 0;
return;
}
if (m_playbackState == SCHEDULED_STATE) {
m_playbackState = PLAYING_STATE;
context()->incrementActiveSourceCount();
}
quantumFrameOffset = startFrame > quantumStartFrame ? startFrame - quantumStartFrame : 0;
quantumFrameOffset = min(quantumFrameOffset, quantumFrameSize);
nonSilentFramesToProcess = quantumFrameSize - quantumFrameOffset;
if (!nonSilentFramesToProcess) {
outputBus->zero();
return;
}
if (quantumFrameOffset) {
for (unsigned i = 0; i < outputBus->numberOfChannels(); ++i)
memset(outputBus->channel(i)->mutableData(), 0, sizeof(float) * quantumFrameOffset);
}
if (m_endTime != UnknownTime && endFrame >= quantumStartFrame && endFrame < quantumEndFrame) {
size_t zeroStartFrame = endFrame - quantumStartFrame;
size_t framesToZero = quantumFrameSize - zeroStartFrame;
bool isSafe = zeroStartFrame < quantumFrameSize && framesToZero <= quantumFrameSize && zeroStartFrame + framesToZero <= quantumFrameSize;
ASSERT(isSafe);
if (isSafe) {
if (framesToZero > nonSilentFramesToProcess)
nonSilentFramesToProcess = 0;
else
nonSilentFramesToProcess -= framesToZero;
for (unsigned i = 0; i < outputBus->numberOfChannels(); ++i)
memset(outputBus->channel(i)->mutableData() + zeroStartFrame, 0, sizeof(float) * framesToZero);
}
finish();
}
return;
}
void AudioScheduledSourceNode::start(double when, ExceptionState& exceptionState)
{
ASSERT(isMainThread());
if (m_playbackState != UNSCHEDULED_STATE) {
exceptionState.throwDOMException(
InvalidStateError,
"cannot call start more than once.");
return;
}
m_startTime = when;
m_playbackState = SCHEDULED_STATE;
}
void AudioScheduledSourceNode::stop(double when, ExceptionState& exceptionState)
{
ASSERT(isMainThread());
if (m_playbackState == UNSCHEDULED_STATE) {
exceptionState.throwDOMException(
InvalidStateError,
"cannot call stop without calling start first.");
} else {
when = max(0.0, when);
m_endTime = when;
}
}
void AudioScheduledSourceNode::setOnended(PassRefPtr<EventListener> listener)
{
m_hasEndedListener = listener;
setAttributeEventListener(EventTypeNames::ended, listener);
}
void AudioScheduledSourceNode::finish()
{
if (m_playbackState != FINISHED_STATE) {
context()->notifyNodeFinishedProcessing(this);
m_playbackState = FINISHED_STATE;
context()->decrementActiveSourceCount();
}
if (m_hasEndedListener) {
OwnPtr<NotifyEndedTask> task = adoptPtr(new NotifyEndedTask(this));
callOnMainThread(&AudioScheduledSourceNode::notifyEndedDispatch, task.leakPtr());
}
}
void AudioScheduledSourceNode::notifyEndedDispatch(void* userData)
{
OwnPtr<NotifyEndedTask> task = adoptPtr(static_cast<NotifyEndedTask*>(userData));
task->notifyEnded();
}
AudioScheduledSourceNode::NotifyEndedTask::NotifyEndedTask(PassRefPtr<AudioScheduledSourceNode> sourceNode)
: m_scheduledNode(sourceNode)
{
}
void AudioScheduledSourceNode::NotifyEndedTask::notifyEnded()
{
RefPtrWillBeRawPtr<Event> event = Event::create(EventTypeNames::ended);
event->setTarget(m_scheduledNode);
m_scheduledNode->dispatchEvent(event.get());
}
}
#endif