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"
|
2015-01-08 16:57:11 +00:00
|
|
|
#include "nsContentUtils.h"
|
2013-09-27 05:22:37 +00:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
2015-01-08 16:57:11 +00:00
|
|
|
#include "nsIEffectiveTLDService.h"
|
2013-09-27 05:22:37 +00:00
|
|
|
#include "nsIRunnable.h"
|
2014-10-15 01:17:03 +00:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
2015-01-08 16:57:11 +00:00
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsNetCID.h"
|
2013-09-27 05:22:37 +00:00
|
|
|
#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"
|
2015-01-09 07:37:00 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-09-27 05:22:37 +00:00
|
|
|
|
|
|
|
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] = {
|
2014-09-02 01:43:03 +00:00
|
|
|
"video/mp4",
|
|
|
|
"audio/mp4",
|
2014-03-05 03:35:47 +00:00
|
|
|
"video/webm",
|
|
|
|
"audio/webm",
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
IsTypeSupported(const nsAString& aType)
|
|
|
|
{
|
|
|
|
if (aType.IsEmpty()) {
|
|
|
|
return NS_ERROR_DOM_INVALID_ACCESS_ERR;
|
|
|
|
}
|
|
|
|
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])) {
|
2014-10-31 10:09:49 +00:00
|
|
|
if ((mimeType.EqualsASCII("video/mp4") ||
|
|
|
|
mimeType.EqualsASCII("audio/mp4")) &&
|
|
|
|
!Preferences::GetBool("media.mediasource.mp4.enabled", false)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((mimeType.EqualsASCII("video/webm") ||
|
|
|
|
mimeType.EqualsASCII("audio/webm")) &&
|
|
|
|
!Preferences::GetBool("media.mediasource.webm.enabled", false)) {
|
|
|
|
break;
|
|
|
|
}
|
2014-03-05 03:35:47 +00:00
|
|
|
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-08-28 03:44:58 +00:00
|
|
|
if (mDecoder) {
|
|
|
|
mDecoder->DetachMediaSource();
|
|
|
|
}
|
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
|
|
|
}
|
2014-11-12 04:11:33 +00:00
|
|
|
MOZ_ASSERT(mDecoder);
|
|
|
|
return mDecoder->GetMediaSourceDuration();
|
2013-06-21 03:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSource::SetDuration(double aDuration, ErrorResult& aRv)
|
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-01-09 01:34:41 +00:00
|
|
|
MSE_API("MediaSource(%p)::SetDuration(aDuration=%f, ErrorResult)", 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;
|
|
|
|
}
|
2015-01-09 01:34:41 +00:00
|
|
|
SetDuration(aDuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSource::SetDuration(double aDuration)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MSE_API("MediaSource(%p)::SetDuration(aDuration=%f)", this, aDuration);
|
2014-11-12 04:11:33 +00:00
|
|
|
mDecoder->SetMediaSourceDuration(aDuration);
|
2013-06-21 03:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-19 05:13:27 +00:00
|
|
|
nsRefPtr<SourceBuffer> sourceBuffer = new SourceBuffer(this, NS_ConvertUTF16toUTF8(mimeType));
|
2014-04-21 13:31:00 +00:00
|
|
|
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();
|
2014-08-26 07:25:09 +00:00
|
|
|
mDecoder->Ended();
|
2014-03-05 03:35:47 +00:00
|
|
|
if (!aError.WasPassed()) {
|
2014-11-12 04:11:33 +00:00
|
|
|
mDecoder->SetMediaSourceDuration(mSourceBuffers->GetHighestBufferedEndTime());
|
2014-08-11 01:21:18 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-01-08 16:57:10 +00:00
|
|
|
/* static */ bool
|
|
|
|
MediaSource::Enabled(JSContext* cx, JSObject* aGlobal)
|
|
|
|
{
|
2015-01-08 16:57:11 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// Don't use aGlobal across Preferences stuff, which the static
|
|
|
|
// analysis thinks can GC.
|
|
|
|
JS::Rooted<JSObject*> global(cx, aGlobal);
|
|
|
|
|
|
|
|
bool enabled = Preferences::GetBool("media.mediasource.enabled");
|
|
|
|
if (!enabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether it's enabled everywhere or just YouTube.
|
|
|
|
bool restrict = Preferences::GetBool("media.mediasource.youtubeonly", false);
|
|
|
|
if (!restrict) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-08 20:34:00 +00:00
|
|
|
// We want to restrict to YouTube only.
|
|
|
|
// We define that as the origin being https://*.youtube.com.
|
|
|
|
// We also support https://*.youtube-nocookie.com.
|
2015-01-08 16:57:11 +00:00
|
|
|
nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(global);
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
if (NS_FAILED(principal->GetURI(getter_AddRefs(uri))) || !uri) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isHttps = false;
|
|
|
|
if (NS_FAILED(uri->SchemeIs("https", &isHttps)) || !isHttps) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEffectiveTLDService> tldServ =
|
|
|
|
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(tldServ, false);
|
|
|
|
|
|
|
|
nsAutoCString eTLDplusOne;
|
2015-01-08 20:34:00 +00:00
|
|
|
if (NS_FAILED(tldServ->GetBaseDomain(uri, 0, eTLDplusOne))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return eTLDplusOne.EqualsLiteral("youtube.com") ||
|
|
|
|
eTLDplusOne.EqualsLiteral("youtube-nocookie.com");
|
2015-01-08 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2014-12-29 03:18:41 +00:00
|
|
|
MOZ_ASSERT(aDecoder->GetOwner());
|
2013-06-21 03:14:42 +00:00
|
|
|
if (mReadyState != MediaSourceReadyState::Closed) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-29 03:18:41 +00:00
|
|
|
MOZ_ASSERT(!mMediaElement);
|
|
|
|
mMediaElement = aDecoder->GetOwner()->GetMediaElement();
|
2014-08-28 03:44:58 +00:00
|
|
|
MOZ_ASSERT(!mDecoder);
|
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",
|
2014-08-28 03:44:58 +00:00
|
|
|
this, mDecoder.get(), mDecoder ? mDecoder->GetOwner() : nullptr);
|
|
|
|
if (!mDecoder) {
|
|
|
|
MOZ_ASSERT(mReadyState == MediaSourceReadyState::Closed);
|
|
|
|
MOZ_ASSERT(mActiveSourceBuffers->IsEmpty() && mSourceBuffers->IsEmpty());
|
|
|
|
return;
|
|
|
|
}
|
2013-09-27 05:22:37 +00:00
|
|
|
mDecoder->DetachMediaSource();
|
|
|
|
mDecoder = nullptr;
|
2014-12-29 03:18:41 +00:00
|
|
|
mMediaElement = nullptr;
|
2014-09-19 03:55:58 +00:00
|
|
|
mFirstSourceBufferInitialized = false;
|
2014-08-28 03:44:58 +00:00
|
|
|
SetReadyState(MediaSourceReadyState::Closed);
|
2014-08-29 00:47:14 +00:00
|
|
|
if (mActiveSourceBuffers) {
|
|
|
|
mActiveSourceBuffers->Clear();
|
|
|
|
}
|
|
|
|
if (mSourceBuffers) {
|
|
|
|
mSourceBuffers->Clear();
|
|
|
|
}
|
2013-06-21 03:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaSource::MediaSource(nsPIDOMWindow* aWindow)
|
2014-04-01 06:13:50 +00:00
|
|
|
: DOMEventTargetHelper(aWindow)
|
2013-09-27 05:22:37 +00:00
|
|
|
, mDecoder(nullptr)
|
2014-10-15 01:17:03 +00:00
|
|
|
, mPrincipal(nullptr)
|
2013-06-21 03:14:42 +00:00
|
|
|
, mReadyState(MediaSourceReadyState::Closed)
|
2014-09-19 03:55:58 +00:00
|
|
|
, mFirstSourceBufferInitialized(false)
|
2013-06-21 03:14:42 +00:00
|
|
|
{
|
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-10-15 01:17:03 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(aWindow);
|
|
|
|
if (sop) {
|
|
|
|
mPrincipal = sop->GetPrincipal();
|
|
|
|
}
|
|
|
|
|
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
|
2014-11-12 04:11:33 +00:00
|
|
|
MediaSource::DurationChange(double aOldDuration, double aNewDuration)
|
2013-06-21 03:14:42 +00:00
|
|
|
{
|
2014-08-11 01:21:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-11-19 04:16:41 +00:00
|
|
|
MSE_DEBUG("MediaSource(%p)::DurationChange(aOldDuration=%f, aNewDuration=%f)", this, aOldDuration, aNewDuration);
|
2014-11-12 04:11:33 +00:00
|
|
|
|
|
|
|
if (aNewDuration < aOldDuration) {
|
2014-11-19 04:16:41 +00:00
|
|
|
mSourceBuffers->RangeRemoval(aNewDuration, aOldDuration);
|
2013-06-21 03:14:42 +00:00
|
|
|
}
|
|
|
|
// TODO: If partial audio frames/text cues exist, clamp duration based on mSourceBuffers.
|
|
|
|
}
|
|
|
|
|
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-09-05 00:04:54 +00:00
|
|
|
void
|
|
|
|
MediaSource::QueueInitializationEvent()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-09-19 03:55:58 +00:00
|
|
|
if (mFirstSourceBufferInitialized) {
|
|
|
|
return;
|
2014-09-05 00:04:54 +00:00
|
|
|
}
|
2014-09-19 03:55:58 +00:00
|
|
|
mFirstSourceBufferInitialized = true;
|
2014-09-05 00:04:54 +00:00
|
|
|
MSE_DEBUG("MediaSource(%p)::QueueInitializationEvent()", this);
|
|
|
|
nsRefPtr<nsIRunnable> task =
|
|
|
|
NS_NewRunnableMethod(this, &MediaSource::InitializationEvent);
|
|
|
|
NS_DispatchToMainThread(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSource::InitializationEvent()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MSE_DEBUG("MediaSource(%p)::InitializationEvent()", this);
|
|
|
|
if (mDecoder) {
|
|
|
|
mDecoder->PrepareReaderInitialization();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 22:47:06 +00:00
|
|
|
#if defined(DEBUG)
|
|
|
|
void
|
|
|
|
MediaSource::Dump(const char* aPath)
|
|
|
|
{
|
|
|
|
char buf[255];
|
|
|
|
PR_snprintf(buf, sizeof(buf), "%s/mediasource-%p", aPath, this);
|
|
|
|
PR_MkDir(buf, 0700);
|
|
|
|
|
|
|
|
if (mSourceBuffers) {
|
|
|
|
mSourceBuffers->Dump(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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,
|
2014-12-29 03:18:41 +00:00
|
|
|
mMediaElement,
|
2014-04-25 16:49:00 +00:00
|
|
|
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
|