SourceBufferList.cpp [plain text]
#include "config.h"
#include "SourceBufferList.h"
#if ENABLE(MEDIA_SOURCE)
#include "Event.h"
#include "EventNames.h"
#include "SourceBuffer.h"
#include <wtf/IsoMallocInlines.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(SourceBufferList);
SourceBufferList::SourceBufferList(ScriptExecutionContext* context)
: ActiveDOMObject(context)
, m_asyncEventQueue(MainThreadGenericEventQueue::create(*this))
{
suspendIfNeeded();
}
SourceBufferList::~SourceBufferList()
{
ASSERT(m_list.isEmpty());
}
void SourceBufferList::add(Ref<SourceBuffer>&& buffer)
{
m_list.append(WTFMove(buffer));
scheduleEvent(eventNames().addsourcebufferEvent);
}
void SourceBufferList::remove(SourceBuffer& buffer)
{
size_t index = m_list.find(&buffer);
if (index == notFound)
return;
m_list.remove(index);
scheduleEvent(eventNames().removesourcebufferEvent);
}
void SourceBufferList::clear()
{
m_list.clear();
scheduleEvent(eventNames().removesourcebufferEvent);
}
void SourceBufferList::swap(Vector<RefPtr<SourceBuffer>>& other)
{
int changeInSize = other.size() - m_list.size();
int addedEntries = 0;
for (auto& sourceBuffer : other) {
if (!m_list.contains(sourceBuffer))
++addedEntries;
}
int removedEntries = addedEntries - changeInSize;
m_list.swap(other);
if (addedEntries)
scheduleEvent(eventNames().addsourcebufferEvent);
if (removedEntries)
scheduleEvent(eventNames().removesourcebufferEvent);
}
void SourceBufferList::scheduleEvent(const AtomString& eventName)
{
auto event = Event::create(eventName, Event::CanBubble::No, Event::IsCancelable::No);
event->setTarget(this);
m_asyncEventQueue->enqueueEvent(WTFMove(event));
}
const char* SourceBufferList::activeDOMObjectName() const
{
return "SourceBufferList";
}
}
#endif