This source file includes following definitions.
- create
- addSourceBuffer
- removeSourceBuffer
- onReadyStateChange
- activeRanges
- isUpdating
- isTypeSupported
- interfaceName
- trace
#include "config.h"
#include "modules/mediasource/MediaSource.h"
#include "bindings/v8/ExceptionState.h"
#include "bindings/v8/ExceptionStatePlaceholder.h"
#include "core/dom/ExceptionCode.h"
#include "core/events/GenericEventQueue.h"
#include "core/html/TimeRanges.h"
#include "modules/mediasource/MediaSourceRegistry.h"
#include "platform/ContentType.h"
#include "platform/Logging.h"
#include "platform/MIMETypeRegistry.h"
#include "public/platform/WebSourceBuffer.h"
#include "wtf/Uint8Array.h"
#include "wtf/text/CString.h"
using blink::WebSourceBuffer;
namespace WebCore {
PassRefPtrWillBeRawPtr<MediaSource> MediaSource::create(ExecutionContext* context)
{
RefPtrWillBeRawPtr<MediaSource> mediaSource(adoptRefWillBeRefCountedGarbageCollected(new MediaSource(context)));
mediaSource->suspendIfNeeded();
return mediaSource.release();
}
MediaSource::MediaSource(ExecutionContext* context)
: MediaSourceBase(context)
{
WTF_LOG(Media, "MediaSource::MediaSource %p", this);
ScriptWrappable::init(this);
m_sourceBuffers = SourceBufferList::create(executionContext(), asyncEventQueue());
m_activeSourceBuffers = SourceBufferList::create(executionContext(), asyncEventQueue());
}
MediaSource::~MediaSource()
{
WTF_LOG(Media, "MediaSource::~MediaSource %p", this);
ASSERT(isClosed());
}
SourceBuffer* MediaSource::addSourceBuffer(const String& type, ExceptionState& exceptionState)
{
WTF_LOG(Media, "MediaSource::addSourceBuffer(%s) %p", type.ascii().data(), this);
if (type.isNull() || type.isEmpty()) {
exceptionState.throwDOMException(InvalidAccessError, "The type provided is empty.");
return 0;
}
if (!isTypeSupported(type)) {
exceptionState.throwDOMException(NotSupportedError, "The type provided ('" + type + "') is unsupported.");
return 0;
}
if (!isOpen()) {
exceptionState.throwDOMException(InvalidStateError, "The MediaSource's readyState is not 'open'.");
return 0;
}
ContentType contentType(type);
Vector<String> codecs = contentType.codecs();
OwnPtr<WebSourceBuffer> webSourceBuffer = createWebSourceBuffer(contentType.type(), codecs, exceptionState);
if (!webSourceBuffer) {
ASSERT(exceptionState.code() == NotSupportedError || exceptionState.code() == QuotaExceededError);
return 0;
}
RefPtrWillBeRawPtr<SourceBuffer> buffer = SourceBuffer::create(webSourceBuffer.release(), this, asyncEventQueue());
m_sourceBuffers->add(buffer);
m_activeSourceBuffers->add(buffer);
return buffer.get();
}
void MediaSource::removeSourceBuffer(SourceBuffer* buffer, ExceptionState& exceptionState)
{
WTF_LOG(Media, "MediaSource::removeSourceBuffer() %p", this);
RefPtr<SourceBuffer> protect(buffer);
if (!buffer) {
exceptionState.throwDOMException(InvalidAccessError, "The SourceBuffer provided is invalid.");
return;
}
if (!m_sourceBuffers->length() || !m_sourceBuffers->contains(buffer)) {
exceptionState.throwDOMException(NotFoundError, "The SourceBuffer provided is not contained in this MediaSource.");
return;
}
buffer->abortIfUpdating();
m_activeSourceBuffers->remove(buffer);
m_sourceBuffers->remove(buffer);
buffer->removedFromMediaSource();
}
void MediaSource::onReadyStateChange(const AtomicString& oldState, const AtomicString& newState)
{
if (isOpen()) {
scheduleEvent(EventTypeNames::sourceopen);
return;
}
if (oldState == openKeyword() && newState == endedKeyword()) {
scheduleEvent(EventTypeNames::sourceended);
return;
}
ASSERT(isClosed());
m_activeSourceBuffers->clear();
for (unsigned long i = 0; i < m_sourceBuffers->length(); ++i)
m_sourceBuffers->item(i)->removedFromMediaSource();
m_sourceBuffers->clear();
scheduleEvent(EventTypeNames::sourceclose);
}
Vector<RefPtr<TimeRanges> > MediaSource::activeRanges() const
{
Vector<RefPtr<TimeRanges> > activeRanges(m_activeSourceBuffers->length());
for (size_t i = 0; i < m_activeSourceBuffers->length(); ++i)
activeRanges[i] = m_activeSourceBuffers->item(i)->buffered(ASSERT_NO_EXCEPTION);
return activeRanges;
}
bool MediaSource::isUpdating() const
{
for (unsigned long i = 0; i < m_sourceBuffers->length(); ++i) {
if (m_sourceBuffers->item(i)->updating())
return true;
}
return false;
}
bool MediaSource::isTypeSupported(const String& type)
{
WTF_LOG(Media, "MediaSource::isTypeSupported(%s)", type.ascii().data());
if (type.isNull() || type.isEmpty())
return false;
ContentType contentType(type);
String codecs = contentType.parameter("codecs");
if (contentType.type().isEmpty())
return false;
return MIMETypeRegistry::isSupportedMediaSourceMIMEType(contentType.type(), codecs);
}
const AtomicString& MediaSource::interfaceName() const
{
return EventTargetNames::MediaSource;
}
void MediaSource::trace(Visitor* visitor)
{
visitor->trace(m_sourceBuffers);
visitor->trace(m_activeSourceBuffers);
MediaSourceBase::trace(visitor);
}
}