2013-06-21 03:14:42 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 "MediaSource.h"
|
|
|
|
|
2013-09-27 05:22:37 +00:00
|
|
|
#include "AsyncEventRunner.h"
|
|
|
|
#include "DecoderTraits.h"
|
2014-08-20 08:07:00 +00:00
|
|
|
#include "MediaSourceUtils.h"
|
2013-06-21 03:14:42 +00:00
|
|
|
#include "SourceBuffer.h"
|
|
|
|
#include "SourceBufferList.h"
|
2013-09-27 05:22:37 +00:00
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "mozilla/FloatingPoint.h"
|
2014-04-14 11:24:00 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-09-27 05:22:37 +00:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2014-08-11 04:32:21 +00:00
|
|
|
#include "mozilla/dom/TimeRanges.h"
|
2013-09-27 05:22:37 +00:00
|
|
|
#include "mozilla/mozalloc.h"
|
2013-08-21 19:28:26 +00:00
|
|
|
#include "nsContentTypeParser.h"
|
2013-09-27 05:22:37 +00:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIEventTarget.h"
|
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2014-08-20 08:14:00 +00:00
|
|
|
#include "nsString.h"
|
2013-09-27 05:22:37 +00:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
|
|
|
|
struct JSContext;
|
|
|
|
class JSObject;
|
2013-06-21 03:14:42 +00:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2014-08-11 01:21:17 +00:00
|
|
|
PRLogModuleInfo* GetMediaSourceLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo* sLogModule;
|
|
|
|
if (!sLogModule) {
|
|
|
|
sLogModule = PR_NewLogModule("MediaSource");
|
|
|
|
}
|
|
|
|
return sLogModule;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRLogModuleInfo* GetMediaSourceAPILog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo* sLogModule;
|
|
|
|
if (!sLogModule) {
|
|
|
|
sLogModule = PR_NewLogModule("MediaSource");
|
|
|
|
}
|
|
|
|
return sLogModule;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MSE_DEBUG(...) PR_LOG(GetMediaSourceLog(), PR_LOG_DEBUG, (__VA_ARGS__))
|
|
|
|
#define MSE_API(...) PR_LOG(GetMediaSourceAPILog(), PR_LOG_DEBUG, (__VA_ARGS__))
|
2013-06-21 03:14:42 +00:00
|
|
|
#else
|
2014-03-05 03:35:46 +00:00
|
|
|
#define MSE_DEBUG(...)
|
2014-08-11 01:21:17 +00:00
|
|
|
#define MSE_API(...)
|
2013-06-21 03:14:42 +00:00
|
|
|
#endif
|
|
|
|
|
2013-09-27 05:22:37 +00:00
|
|
|
// Arbitrary limit.
|
|
|
|
static const unsigned int MAX_SOURCE_BUFFERS = 16;
|
|
|
|
|
2013-06-21 03:14:42 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
2014-03-05 03:35:47 +00:00
|
|
|
static const char* const gMediaSourceTypes[6] = {
|
|
|
|
"video/webm",
|
|
|
|
"audio/webm",
|
2014-04-23 09:25:00 +00:00
|
|
|
// XXX: Disabled other codecs temporarily to allow WebM testing. For now, set
|
|
|
|
// the developer-only media.mediasource.ignore_codecs pref to true to test other
|
|
|
|
// codecs, and expect things to be broken.
|
|
|
|
#if 0
|
2014-03-05 03:35:47 +00:00
|
|
|
"video/mp4",
|
|
|
|
"audio/mp4",
|
|
|
|
"audio/mpeg",
|
2014-04-23 09:25:00 +00:00
|
|
|
#endif
|
2014-03-05 03:35:47 +00:00
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
IsTypeSupported(const nsAString& aType)
|
|
|
|
{
|
|
|
|
if (aType.IsEmpty()) {
|
|
|
|
return NS_ERROR_DOM_INVALID_ACCESS_ERR;
|
|
|
|
}
|
2014-07-04 08:31:20 +00:00
|
|
|
if (Preferences::GetBool("media.mediasource.ignore_codecs", false)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-03-05 03:35:47 +00:00
|
|
|
// TODO: Further restrict this to formats in the spec.
|
|
|
|
nsContentTypeParser parser(aType);
|
|
|
|
nsAutoString mimeType;
|
|
|
|
nsresult rv = parser.GetType(mimeType);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
|
|
|
|
}
|
|
|
|
bool found = false;
|
|
|
|
for (uint32_t i = 0; gMediaSourceTypes[i]; ++i) {
|
|
|
|
if (mimeType.EqualsASCII(gMediaSourceTypes[i])) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
|
|
|
|
}
|
|
|
|
// Check aType against HTMLMediaElement list of MIME types. Since we've
|
|
|
|
// already restricted the container format, this acts as a specific check
|
|
|
|
// of any specified "codecs" parameter of aType.
|
|
|
|
if (dom::HTMLMediaElement::GetCanPlay(aType) == CANPLAY_NO) {
|
|
|
|
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-27 05:22:37 +00:00
|
|
|
namespace dom {
|
2013-06-21 03:14:42 +00:00
|
|
|
|
|
|
|
/* static */ already_AddRefed<MediaSource>
|
2013-08-23 05:17:08 +00:00
|
|
|
MediaSource::Constructor(const GlobalObject& aGlobal,
|
|
|
|
ErrorResult& aRv)
|
2013-06-21 03:14:42 +00:00
|
|
|
{
|
2013-08-23 05:17:08 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-06-21 03:14:42 +00:00
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<MediaSource> mediaSource = new MediaSource(window);
|
|
|
|
return mediaSource.forget();
|
|
|
|
}
|
|
|
|
|
2014-06-14 14:04:01 +00:00
|
|
|
MediaSource::~MediaSource()
|
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-08-11 01:21:17 +00:00
|
|
|
MSE_API("MediaSource(%p)::~MediaSource()", this);
|
2014-06-14 14:04:01 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 03:14:42 +00:00
|
|
|
SourceBufferList*
|
|
|
|
MediaSource::SourceBuffers()
|
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-06-21 03:14:42 +00:00
|
|
|
MOZ_ASSERT_IF(mReadyState == MediaSourceReadyState::Closed, mSourceBuffers->IsEmpty());
|
|
|
|
return mSourceBuffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceBufferList*
|
|
|
|
MediaSource::ActiveSourceBuffers()
|
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-06-21 03:14:42 +00:00
|
|
|
MOZ_ASSERT_IF(mReadyState == MediaSourceReadyState::Closed, mActiveSourceBuffers->IsEmpty());
|
|
|
|
return mActiveSourceBuffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaSourceReadyState
|
|
|
|
MediaSource::ReadyState()
|
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-06-21 03:14:42 +00:00
|
|
|
return mReadyState;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
MediaSource::Duration()
|
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-06-21 03:14:42 +00:00
|
|
|
if (mReadyState == MediaSourceReadyState::Closed) {
|
2014-02-27 15:23:16 +00:00
|
|
|
return UnspecifiedNaN<double>();
|
2013-06-21 03:14:42 +00:00
|
|
|
}
|
|
|
|
return mDuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSource::SetDuration(double aDuration, ErrorResult& aRv)
|
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-08-11 01:21:17 +00:00
|
|
|
MSE_API("MediaSource(%p)::SetDuration(aDuration=%f)", this, aDuration);
|
2013-06-21 03:14:42 +00:00
|
|
|
if (aDuration < 0 || IsNaN(aDuration)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mReadyState != MediaSourceReadyState::Open ||
|
|
|
|
mSourceBuffers->AnyUpdating()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DurationChange(aDuration, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<SourceBuffer>
|
|
|
|
MediaSource::AddSourceBuffer(const nsAString& aType, ErrorResult& aRv)
|
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-03-05 03:35:47 +00:00
|
|
|
nsresult rv = mozilla::IsTypeSupported(aType);
|
2014-08-11 01:21:17 +00:00
|
|
|
MSE_API("MediaSource(%p)::AddSourceBuffer(aType=%s)%s",
|
|
|
|
this, NS_ConvertUTF16toUTF8(aType).get(),
|
|
|
|
rv == NS_OK ? "" : " [not supported]");
|
2014-03-05 03:35:47 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
2013-06-21 03:14:42 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2013-09-27 05:22:37 +00:00
|
|
|
if (mSourceBuffers->Length() >= MAX_SOURCE_BUFFERS) {
|
2013-06-21 03:14:42 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_QUOTA_EXCEEDED_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (mReadyState != MediaSourceReadyState::Open) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-09-27 05:22:37 +00:00
|
|
|
nsContentTypeParser parser(aType);
|
|
|
|
nsAutoString mimeType;
|
2014-03-05 03:35:47 +00:00
|
|
|
rv = parser.GetType(mimeType);
|
2013-09-27 05:22:37 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-04-21 13:31:00 +00:00
|
|
|
nsRefPtr<SourceBuffer> sourceBuffer = SourceBuffer::Create(this, NS_ConvertUTF16toUTF8(mimeType));
|
|
|
|
if (!sourceBuffer) {
|
2014-04-14 11:24:00 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE); // XXX need a better error here
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-06-21 03:14:42 +00:00
|
|
|
mSourceBuffers->Append(sourceBuffer);
|
2014-04-23 07:44:00 +00:00
|
|
|
mActiveSourceBuffers->Append(sourceBuffer);
|
2014-08-11 01:21:17 +00:00
|
|
|
MSE_DEBUG("MediaSource(%p)::AddSourceBuffer() sourceBuffer=%p", this, sourceBuffer.get());
|
2013-06-21 03:14:42 +00:00
|
|
|
return sourceBuffer.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSource::RemoveSourceBuffer(SourceBuffer& aSourceBuffer, ErrorResult& aRv)
|
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-06-21 03:14:42 +00:00
|
|
|
SourceBuffer* sourceBuffer = &aSourceBuffer;
|
2014-08-11 01:21:17 +00:00
|
|
|
MSE_API("MediaSource(%p)::RemoveSourceBuffer(aSourceBuffer=%p)", this, sourceBuffer);
|
2013-06-21 03:14:42 +00:00
|
|
|
if (!mSourceBuffers->Contains(sourceBuffer)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_FOUND_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sourceBuffer->Updating()) {
|
|
|
|
// TODO:
|
|
|
|
// abort stream append loop (if running)
|
|
|
|
// set updating to false
|
|
|
|
// fire "abort" at sourceBuffer
|
|
|
|
// fire "updateend" at sourceBuffer
|
|
|
|
}
|
|
|
|
// TODO:
|
|
|
|
// For all sourceBuffer audioTracks, videoTracks, textTracks:
|
|
|
|
// set sourceBuffer to null
|
|
|
|
// remove sourceBuffer video, audio, text Tracks from MediaElement tracks
|
|
|
|
// remove sourceBuffer video, audio, text Tracks and fire "removetrack" at affected lists
|
|
|
|
// fire "removetrack" at modified MediaElement track lists
|
|
|
|
// If removed enabled/selected, fire "change" at affected MediaElement list.
|
|
|
|
if (mActiveSourceBuffers->Contains(sourceBuffer)) {
|
|
|
|
mActiveSourceBuffers->Remove(sourceBuffer);
|
|
|
|
}
|
|
|
|
mSourceBuffers->Remove(sourceBuffer);
|
|
|
|
// TODO: Free all resources associated with sourceBuffer
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSource::EndOfStream(const Optional<MediaSourceEndOfStreamError>& aError, ErrorResult& aRv)
|
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-08-11 01:21:17 +00:00
|
|
|
MSE_API("MediaSource(%p)::EndOfStream(aError=%d)",
|
|
|
|
this, aError.WasPassed() ? uint32_t(aError.Value()) : 0);
|
2013-06-21 03:14:42 +00:00
|
|
|
if (mReadyState != MediaSourceReadyState::Open ||
|
|
|
|
mSourceBuffers->AnyUpdating()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2014-03-05 03:35:47 +00:00
|
|
|
|
|
|
|
SetReadyState(MediaSourceReadyState::Ended);
|
|
|
|
mSourceBuffers->Ended();
|
|
|
|
if (!aError.WasPassed()) {
|
2014-08-11 01:21:18 +00:00
|
|
|
DurationChange(mSourceBuffers->GetHighestBufferedEndTime(), aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-05 03:35:47 +00:00
|
|
|
// TODO:
|
2014-08-11 01:21:18 +00:00
|
|
|
// Notify media element that all data is now available.
|
2014-03-05 03:35:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (aError.Value()) {
|
|
|
|
case MediaSourceEndOfStreamError::Network:
|
|
|
|
// TODO: If media element has a readyState of:
|
|
|
|
// HAVE_NOTHING -> run resource fetch algorithm
|
|
|
|
// > HAVE_NOTHING -> run "interrupted" steps of resource fetch
|
|
|
|
break;
|
|
|
|
case MediaSourceEndOfStreamError::Decode:
|
|
|
|
// TODO: If media element has a readyState of:
|
|
|
|
// HAVE_NOTHING -> run "unsupported" steps of resource fetch
|
|
|
|
// > HAVE_NOTHING -> run "corrupted" steps of resource fetch
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
}
|
2013-06-21 03:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool
|
2014-03-05 03:35:47 +00:00
|
|
|
MediaSource::IsTypeSupported(const GlobalObject&, const nsAString& aType)
|
2013-06-21 03:14:42 +00:00
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-04-14 11:24:00 +00:00
|
|
|
nsresult rv = mozilla::IsTypeSupported(aType);
|
2014-08-11 01:21:17 +00:00
|
|
|
MSE_API("MediaSource::IsTypeSupported(aType=%s)%s",
|
|
|
|
NS_ConvertUTF16toUTF8(aType).get(), rv == NS_OK ? "" : " [not supported]");
|
2014-04-14 11:24:00 +00:00
|
|
|
return NS_SUCCEEDED(rv);
|
2013-06-21 03:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-09-27 05:22:37 +00:00
|
|
|
MediaSource::Attach(MediaSourceDecoder* aDecoder)
|
2013-06-21 03:14:42 +00:00
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-08-11 01:21:17 +00:00
|
|
|
MSE_DEBUG("MediaSource(%p)::Attach(aDecoder=%p) owner=%p", this, aDecoder, aDecoder->GetOwner());
|
2013-09-27 05:22:37 +00:00
|
|
|
MOZ_ASSERT(aDecoder);
|
2013-06-21 03:14:42 +00:00
|
|
|
if (mReadyState != MediaSourceReadyState::Closed) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-27 05:22:37 +00:00
|
|
|
mDecoder = aDecoder;
|
|
|
|
mDecoder->AttachMediaSource(this);
|
2013-06-21 03:14:42 +00:00
|
|
|
SetReadyState(MediaSourceReadyState::Open);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-27 05:22:37 +00:00
|
|
|
MediaSource::Detach()
|
2013-06-21 03:14:42 +00:00
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-08-11 01:21:17 +00:00
|
|
|
MSE_DEBUG("MediaSource(%p)::Detach() mDecoder=%p owner=%p",
|
|
|
|
this, mDecoder.get(), mDecoder->GetOwner());
|
2013-09-27 05:22:37 +00:00
|
|
|
MOZ_ASSERT(mDecoder);
|
|
|
|
mDecoder->DetachMediaSource();
|
|
|
|
mDecoder = nullptr;
|
2014-02-27 15:23:16 +00:00
|
|
|
mDuration = UnspecifiedNaN<double>();
|
2013-06-21 03:14:42 +00:00
|
|
|
mActiveSourceBuffers->Clear();
|
2013-09-27 05:22:37 +00:00
|
|
|
mSourceBuffers->Clear();
|
2013-06-21 03:14:42 +00:00
|
|
|
SetReadyState(MediaSourceReadyState::Closed);
|
|
|
|
}
|
|
|
|
|
2014-08-11 04:32:21 +00:00
|
|
|
void
|
|
|
|
MediaSource::GetBuffered(TimeRanges* aBuffered)
|
|
|
|
{
|
2014-08-20 08:07:00 +00:00
|
|
|
MOZ_ASSERT(aBuffered->Length() == 0);
|
2014-08-11 04:32:21 +00:00
|
|
|
if (mActiveSourceBuffers->IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-20 08:07:00 +00:00
|
|
|
double highestEndTime = 0;
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<TimeRanges>> activeRanges;
|
2014-08-11 04:32:21 +00:00
|
|
|
for (uint32_t i = 0; i < mActiveSourceBuffers->Length(); ++i) {
|
|
|
|
bool found;
|
|
|
|
SourceBuffer* sourceBuffer = mActiveSourceBuffers->IndexedGetter(i, found);
|
|
|
|
|
|
|
|
ErrorResult dummy;
|
2014-08-20 08:07:00 +00:00
|
|
|
*activeRanges.AppendElement() = sourceBuffer->GetBuffered(dummy);
|
2014-08-11 04:32:21 +00:00
|
|
|
|
2014-08-20 08:07:00 +00:00
|
|
|
highestEndTime = std::max(highestEndTime, activeRanges.LastElement()->GetEndTime());
|
2014-08-11 04:32:21 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 08:07:00 +00:00
|
|
|
TimeRanges* intersectionRanges = aBuffered;
|
|
|
|
intersectionRanges->Add(0, highestEndTime);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < activeRanges.Length(); ++i) {
|
|
|
|
TimeRanges* sourceRanges = activeRanges[i];
|
2014-08-11 04:32:21 +00:00
|
|
|
|
|
|
|
if (mReadyState == MediaSourceReadyState::Ended) {
|
2014-08-20 08:07:00 +00:00
|
|
|
// Set the end time on the last range to highestEndTime by adding a
|
|
|
|
// new range spanning the current end time to highestEndTime, which
|
|
|
|
// Normalize() will then merge with the old last range.
|
|
|
|
sourceRanges->Add(sourceRanges->GetEndTime(), highestEndTime);
|
|
|
|
sourceRanges->Normalize();
|
2014-08-11 04:32:21 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 08:07:00 +00:00
|
|
|
intersectionRanges->Intersection(sourceRanges);
|
2014-08-11 04:32:21 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 08:07:00 +00:00
|
|
|
MSE_DEBUG("MediaSource(%p)::GetBuffered ranges=%s", this, DumpTimeRanges(intersectionRanges).get());
|
2014-08-11 04:32:21 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 03:14:42 +00:00
|
|
|
MediaSource::MediaSource(nsPIDOMWindow* aWindow)
|
2014-04-01 06:13:50 +00:00
|
|
|
: DOMEventTargetHelper(aWindow)
|
2014-02-27 15:23:16 +00:00
|
|
|
, mDuration(UnspecifiedNaN<double>())
|
2013-09-27 05:22:37 +00:00
|
|
|
, mDecoder(nullptr)
|
2013-06-21 03:14:42 +00:00
|
|
|
, mReadyState(MediaSourceReadyState::Closed)
|
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-06-21 03:14:42 +00:00
|
|
|
mSourceBuffers = new SourceBufferList(this);
|
|
|
|
mActiveSourceBuffers = new SourceBufferList(this);
|
2014-08-11 01:21:17 +00:00
|
|
|
MSE_API("MediaSource(%p)::MediaSource(aWindow=%p) mSourceBuffers=%p mActiveSourceBuffers=%p",
|
|
|
|
this, aWindow, mSourceBuffers.get(), mActiveSourceBuffers.get());
|
2013-06-21 03:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSource::SetReadyState(MediaSourceReadyState aState)
|
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-06-21 03:14:42 +00:00
|
|
|
MOZ_ASSERT(aState != mReadyState);
|
2014-08-11 01:21:17 +00:00
|
|
|
MSE_DEBUG("MediaSource(%p)::SetReadyState(aState=%d) mReadyState=%d", this, aState, mReadyState);
|
2013-06-21 03:14:42 +00:00
|
|
|
|
2013-10-05 08:03:50 +00:00
|
|
|
MediaSourceReadyState oldState = mReadyState;
|
|
|
|
mReadyState = aState;
|
|
|
|
|
|
|
|
if (mReadyState == MediaSourceReadyState::Open &&
|
|
|
|
(oldState == MediaSourceReadyState::Closed ||
|
|
|
|
oldState == MediaSourceReadyState::Ended)) {
|
2013-06-21 03:14:42 +00:00
|
|
|
QueueAsyncSimpleEvent("sourceopen");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-05 08:03:50 +00:00
|
|
|
if (mReadyState == MediaSourceReadyState::Ended &&
|
|
|
|
oldState == MediaSourceReadyState::Open) {
|
2013-06-21 03:14:42 +00:00
|
|
|
QueueAsyncSimpleEvent("sourceended");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-05 08:03:50 +00:00
|
|
|
if (mReadyState == MediaSourceReadyState::Closed &&
|
|
|
|
(oldState == MediaSourceReadyState::Open ||
|
|
|
|
oldState == MediaSourceReadyState::Ended)) {
|
2013-06-21 03:14:42 +00:00
|
|
|
QueueAsyncSimpleEvent("sourceclose");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING("Invalid MediaSource readyState transition");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSource::DispatchSimpleEvent(const char* aName)
|
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-08-11 01:21:17 +00:00
|
|
|
MSE_API("MediaSource(%p) Dispatch event '%s'", this, aName);
|
2013-06-21 03:14:42 +00:00
|
|
|
DispatchTrustedEvent(NS_ConvertUTF8toUTF16(aName));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSource::QueueAsyncSimpleEvent(const char* aName)
|
|
|
|
{
|
2014-08-11 01:21:17 +00:00
|
|
|
MSE_DEBUG("MediaSource(%p) Queuing event '%s'", this, aName);
|
2013-09-27 05:22:37 +00:00
|
|
|
nsCOMPtr<nsIRunnable> event = new AsyncEventRunner<MediaSource>(this, aName);
|
2014-05-23 19:53:17 +00:00
|
|
|
NS_DispatchToMainThread(event);
|
2013-06-21 03:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSource::DurationChange(double aNewDuration, ErrorResult& aRv)
|
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-08-11 01:21:17 +00:00
|
|
|
MSE_DEBUG("MediaSource(%p)::DurationChange(aNewDuration=%f)", this, aNewDuration);
|
2013-06-21 03:14:42 +00:00
|
|
|
if (mDuration == aNewDuration) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
double oldDuration = mDuration;
|
|
|
|
mDuration = aNewDuration;
|
|
|
|
if (aNewDuration < oldDuration) {
|
|
|
|
mSourceBuffers->Remove(aNewDuration, oldDuration, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: If partial audio frames/text cues exist, clamp duration based on mSourceBuffers.
|
|
|
|
// TODO: Update media element's duration and run element's duration change algorithm.
|
|
|
|
}
|
|
|
|
|
2014-02-28 00:54:48 +00:00
|
|
|
void
|
|
|
|
MediaSource::NotifyEvicted(double aStart, double aEnd)
|
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-08-11 01:21:17 +00:00
|
|
|
MSE_DEBUG("MediaSource(%p)::NotifyEvicted(aStart=%f, aEnd=%f)", this, aStart, aEnd);
|
2014-02-28 00:54:48 +00:00
|
|
|
// Cycle through all SourceBuffers and tell them to evict data in
|
|
|
|
// the given range.
|
|
|
|
mSourceBuffers->Evict(aStart, aEnd);
|
|
|
|
}
|
|
|
|
|
2014-08-11 01:21:17 +00:00
|
|
|
nsPIDOMWindow*
|
|
|
|
MediaSource::GetParentObject() const
|
|
|
|
{
|
|
|
|
return GetOwner();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
MediaSource::WrapObject(JSContext* aCx)
|
|
|
|
{
|
|
|
|
return MediaSourceBinding::Wrap(aCx, this);
|
2014-06-02 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 16:49:00 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(MediaSource, DOMEventTargetHelper,
|
|
|
|
mSourceBuffers, mActiveSourceBuffers)
|
2013-06-21 03:14:42 +00:00
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(MediaSource, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(MediaSource, DOMEventTargetHelper)
|
2013-06-21 03:14:42 +00:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MediaSource)
|
2013-06-21 03:15:15 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(mozilla::dom::MediaSource)
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2013-06-21 03:14:42 +00:00
|
|
|
|
|
|
|
} // namespace dom
|
2013-09-27 05:22:37 +00:00
|
|
|
|
2013-06-21 03:14:42 +00:00
|
|
|
} // namespace mozilla
|