2015-05-03 22:32:37 +03: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: */
|
2013-10-23 22:45:00 +04:00
|
|
|
/* 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 "mozilla/dom/TextTrackManager.h"
|
2018-12-13 23:04:46 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
|
|
|
#include "mozilla/CycleCollectedJSContext.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
#include "mozilla/dom/Event.h"
|
2013-10-23 22:45:00 +04:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2013-10-25 08:14:36 +04:00
|
|
|
#include "mozilla/dom/HTMLTrackElement.h"
|
2014-04-07 23:42:33 +04:00
|
|
|
#include "mozilla/dom/HTMLVideoElement.h"
|
2013-12-12 21:02:17 +04:00
|
|
|
#include "mozilla/dom/TextTrack.h"
|
|
|
|
#include "mozilla/dom/TextTrackCue.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2017-03-27 11:51:17 +03:00
|
|
|
#include "nsGlobalWindow.h"
|
2013-12-12 21:02:17 +04:00
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsIWebVTTParserWrapper.h"
|
2018-12-13 23:04:46 +03:00
|
|
|
#include "nsVariant.h"
|
|
|
|
#include "nsVideoFrame.h"
|
2013-10-23 22:45:00 +04:00
|
|
|
|
2019-03-15 02:21:28 +03:00
|
|
|
static mozilla::LazyLogModule gTextTrackLog("WebVTT");
|
2019-03-15 02:24:32 +03:00
|
|
|
|
|
|
|
#define WEBVTT_LOG(msg, ...) \
|
|
|
|
MOZ_LOG(gTextTrackLog, LogLevel::Debug, ("TextTrackManager=%p, " msg, this, ##__VA_ARGS__))
|
|
|
|
#define WEBVTT_LOGV(msg, ...) \
|
|
|
|
MOZ_LOG(gTextTrackLog, LogLevel::Verbose, ("TextTrackManager=%p, " msg, this, ##__VA_ARGS__))
|
2016-08-25 05:58:49 +03:00
|
|
|
|
2013-10-23 22:45:00 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2016-06-01 10:13:45 +03:00
|
|
|
NS_IMPL_ISUPPORTS(TextTrackManager::ShutdownObserverProxy, nsIObserver);
|
|
|
|
|
2018-11-20 02:16:24 +03:00
|
|
|
void TextTrackManager::ShutdownObserverProxy::Unregister() {
|
|
|
|
nsContentUtils::UnregisterShutdownObserver(this);
|
|
|
|
mManager = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-02-27 21:47:23 +04:00
|
|
|
CompareTextTracks::CompareTextTracks(HTMLMediaElement* aMediaElement) {
|
|
|
|
mMediaElement = aMediaElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t CompareTextTracks::TrackChildPosition(TextTrack* aTextTrack) const {
|
2017-02-01 04:24:17 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aTextTrack);
|
2014-02-27 21:47:23 +04:00
|
|
|
HTMLTrackElement* trackElement = aTextTrack->GetTrackElement();
|
|
|
|
if (!trackElement) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-01-23 16:30:18 +03:00
|
|
|
return mMediaElement->ComputeIndexOf(trackElement);
|
2014-02-27 21:47:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CompareTextTracks::Equals(TextTrack* aOne, TextTrack* aTwo) const {
|
|
|
|
// Two tracks can never be equal. If they have corresponding TrackElements
|
|
|
|
// they would need to occupy the same tree position (impossible) and in the
|
|
|
|
// case of tracks coming from AddTextTrack source we put the newest at the
|
|
|
|
// last position, so they won't be equal as well.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CompareTextTracks::LessThan(TextTrack* aOne, TextTrack* aTwo) const {
|
2017-02-01 04:24:17 +03:00
|
|
|
// Protect against nullptr TextTrack objects; treat them as
|
|
|
|
// sorting toward the end.
|
|
|
|
if (!aOne) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!aTwo) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-27 21:47:23 +04:00
|
|
|
TextTrackSource sourceOne = aOne->GetTextTrackSource();
|
|
|
|
TextTrackSource sourceTwo = aTwo->GetTextTrackSource();
|
|
|
|
if (sourceOne != sourceTwo) {
|
2016-07-11 05:51:13 +03:00
|
|
|
return sourceOne == TextTrackSource::Track ||
|
2014-02-27 21:47:23 +04:00
|
|
|
(sourceOne == AddTextTrack && sourceTwo == MediaResourceSpecific);
|
|
|
|
}
|
|
|
|
switch (sourceOne) {
|
|
|
|
case Track: {
|
|
|
|
int32_t positionOne = TrackChildPosition(aOne);
|
|
|
|
int32_t positionTwo = TrackChildPosition(aTwo);
|
|
|
|
// If either position one or positiontwo are -1 then something has gone
|
|
|
|
// wrong. In this case we should just put them at the back of the list.
|
|
|
|
return positionOne != -1 && positionTwo != -1 &&
|
|
|
|
positionOne < positionTwo;
|
|
|
|
}
|
|
|
|
case AddTextTrack:
|
|
|
|
// For AddTextTrack sources the tracks will already be in the correct
|
|
|
|
// relative order in the source array. Assume we're called in iteration
|
|
|
|
// order and can therefore always report aOne < aTwo to maintain the
|
|
|
|
// original temporal ordering.
|
|
|
|
return true;
|
|
|
|
case MediaResourceSpecific:
|
|
|
|
// No rules for Media Resource Specific tracks yet.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-25 20:49:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(TextTrackManager, mMediaElement, mTextTracks,
|
2016-06-01 08:35:56 +03:00
|
|
|
mPendingTextTracks, mNewCues, mLastActiveCues)
|
2014-04-07 23:42:33 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TextTrackManager)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(TextTrackManager)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(TextTrackManager)
|
2013-10-23 22:45:00 +04:00
|
|
|
|
2013-12-12 21:02:17 +04:00
|
|
|
StaticRefPtr<nsIWebVTTParserWrapper> TextTrackManager::sParserWrapper;
|
|
|
|
|
2013-10-23 22:45:00 +04:00
|
|
|
TextTrackManager::TextTrackManager(HTMLMediaElement* aMediaElement)
|
|
|
|
: mMediaElement(aMediaElement),
|
2016-06-01 08:35:56 +03:00
|
|
|
mHasSeeked(false),
|
|
|
|
mLastTimeMarchesOnCalled(0.0),
|
|
|
|
mTimeMarchesOnDispatched(false),
|
2016-09-27 13:19:17 +03:00
|
|
|
mUpdateCueDisplayDispatched(false),
|
2014-03-01 00:40:00 +04:00
|
|
|
performedTrackSelection(false),
|
2016-07-11 05:51:08 +03:00
|
|
|
mCueTelemetryReported(false),
|
2016-06-01 10:13:45 +03:00
|
|
|
mShutdown(false) {
|
2015-01-12 14:07:38 +03:00
|
|
|
nsISupports* parentObject = mMediaElement->OwnerDoc()->GetParentObject();
|
2014-04-07 21:58:38 +04:00
|
|
|
|
2015-01-12 14:07:38 +03:00
|
|
|
NS_ENSURE_TRUE_VOID(parentObject);
|
2019-03-15 02:24:32 +03:00
|
|
|
WEBVTT_LOG("Create TextTrackManager");
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(parentObject);
|
2014-04-07 21:58:38 +04:00
|
|
|
mNewCues = new TextTrackCueList(window);
|
2016-06-01 08:35:56 +03:00
|
|
|
mLastActiveCues = new TextTrackCueList(window);
|
2014-04-07 21:58:38 +04:00
|
|
|
mTextTracks = new TextTrackList(window, this);
|
|
|
|
mPendingTextTracks = new TextTrackList(window, this);
|
2013-12-12 21:02:17 +04:00
|
|
|
|
|
|
|
if (!sParserWrapper) {
|
|
|
|
nsCOMPtr<nsIWebVTTParserWrapper> parserWrapper =
|
|
|
|
do_CreateInstance(NS_WEBVTTPARSERWRAPPER_CONTRACTID);
|
2017-08-09 12:30:52 +03:00
|
|
|
MOZ_ASSERT(parserWrapper, "Can't create nsIWebVTTParserWrapper");
|
2013-12-12 21:02:17 +04:00
|
|
|
sParserWrapper = parserWrapper;
|
|
|
|
ClearOnShutdown(&sParserWrapper);
|
|
|
|
}
|
2016-06-01 10:13:45 +03:00
|
|
|
mShutdownProxy = new ShutdownObserverProxy(this);
|
2013-10-23 22:45:00 +04:00
|
|
|
}
|
|
|
|
|
2014-06-25 06:09:15 +04:00
|
|
|
TextTrackManager::~TextTrackManager() {
|
2019-03-15 02:24:32 +03:00
|
|
|
WEBVTT_LOG("~TextTrackManager");
|
2018-11-20 02:16:24 +03:00
|
|
|
mShutdownProxy->Unregister();
|
2014-06-25 06:09:15 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 14:07:38 +03:00
|
|
|
TextTrackList* TextTrackManager::GetTextTracks() const { return mTextTracks; }
|
2013-10-23 22:45:00 +04:00
|
|
|
|
|
|
|
already_AddRefed<TextTrack> TextTrackManager::AddTextTrack(
|
|
|
|
TextTrackKind aKind, const nsAString& aLabel, const nsAString& aLanguage,
|
2014-03-13 22:41:21 +04:00
|
|
|
TextTrackMode aMode, TextTrackReadyState aReadyState,
|
2014-02-27 21:47:23 +04:00
|
|
|
TextTrackSource aTextTrackSource) {
|
2014-04-07 21:58:38 +04:00
|
|
|
if (!mMediaElement || !mTextTracks) {
|
2014-02-27 21:47:23 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-07-11 05:51:13 +03:00
|
|
|
RefPtr<TextTrack> track = mTextTracks->AddTextTrack(
|
2014-03-13 22:41:21 +04:00
|
|
|
aKind, aLabel, aLanguage, aMode, aReadyState, aTextTrackSource,
|
|
|
|
CompareTextTracks(mMediaElement));
|
2019-03-15 02:24:32 +03:00
|
|
|
WEBVTT_LOG("AddTextTrack %p kind %" PRIu32 " Label %s Language %s",
|
|
|
|
track.get(), static_cast<uint32_t>(aKind), NS_ConvertUTF16toUTF8(aLabel).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(aLanguage).get());
|
2016-07-11 05:51:13 +03:00
|
|
|
AddCues(track);
|
|
|
|
ReportTelemetryForTrack(track);
|
2014-03-01 00:40:00 +04:00
|
|
|
|
2016-07-11 05:51:13 +03:00
|
|
|
if (aTextTrackSource == TextTrackSource::Track) {
|
2017-06-12 22:34:10 +03:00
|
|
|
RefPtr<nsIRunnable> task = NewRunnableMethod(
|
|
|
|
"dom::TextTrackManager::HonorUserPreferencesForTrackSelection", this,
|
|
|
|
&TextTrackManager::HonorUserPreferencesForTrackSelection);
|
2017-01-23 06:12:22 +03:00
|
|
|
nsContentUtils::RunInStableState(task.forget());
|
2014-03-01 00:40:00 +04:00
|
|
|
}
|
|
|
|
|
2016-07-11 05:51:13 +03:00
|
|
|
return track.forget();
|
2013-10-23 22:45:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextTrackManager::AddTextTrack(TextTrack* aTextTrack) {
|
2014-04-07 21:58:38 +04:00
|
|
|
if (!mMediaElement || !mTextTracks) {
|
2014-02-27 21:47:23 +04:00
|
|
|
return;
|
|
|
|
}
|
2019-03-15 02:24:32 +03:00
|
|
|
WEBVTT_LOG("AddTextTrack TextTrack %p", aTextTrack);
|
2014-02-27 21:47:23 +04:00
|
|
|
mTextTracks->AddTextTrack(aTextTrack, CompareTextTracks(mMediaElement));
|
2014-01-06 22:03:50 +04:00
|
|
|
AddCues(aTextTrack);
|
2016-07-11 05:51:08 +03:00
|
|
|
ReportTelemetryForTrack(aTextTrack);
|
|
|
|
|
2016-07-11 05:51:13 +03:00
|
|
|
if (aTextTrack->GetTextTrackSource() == TextTrackSource::Track) {
|
2017-06-12 22:34:10 +03:00
|
|
|
RefPtr<nsIRunnable> task = NewRunnableMethod(
|
|
|
|
"dom::TextTrackManager::HonorUserPreferencesForTrackSelection", this,
|
|
|
|
&TextTrackManager::HonorUserPreferencesForTrackSelection);
|
2017-01-23 06:12:22 +03:00
|
|
|
nsContentUtils::RunInStableState(task.forget());
|
2014-03-01 00:40:00 +04:00
|
|
|
}
|
2014-01-06 22:03:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextTrackManager::AddCues(TextTrack* aTextTrack) {
|
2014-04-07 21:58:38 +04:00
|
|
|
if (!mNewCues) {
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOG("AddCues mNewCues is null");
|
2014-04-07 21:58:38 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-06 22:03:50 +04:00
|
|
|
TextTrackCueList* cueList = aTextTrack->GetCues();
|
|
|
|
if (cueList) {
|
|
|
|
bool dummy;
|
2019-03-15 02:24:32 +03:00
|
|
|
WEBVTT_LOGV("AddCues, CuesNum=%d", cueList->Length());
|
2014-01-06 22:03:50 +04:00
|
|
|
for (uint32_t i = 0; i < cueList->Length(); ++i) {
|
|
|
|
mNewCues->AddCue(*cueList->IndexedGetter(i, dummy));
|
|
|
|
}
|
2019-03-08 06:12:42 +03:00
|
|
|
TimeMarchesOn();
|
2014-01-06 22:03:50 +04:00
|
|
|
}
|
2013-10-23 22:45:00 +04:00
|
|
|
}
|
|
|
|
|
2013-10-25 08:14:36 +04:00
|
|
|
void TextTrackManager::RemoveTextTrack(TextTrack* aTextTrack,
|
|
|
|
bool aPendingListOnly) {
|
2014-04-07 21:58:38 +04:00
|
|
|
if (!mPendingTextTracks || !mTextTracks) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-15 02:24:32 +03:00
|
|
|
WEBVTT_LOG("RemoveTextTrack TextTrack %p", aTextTrack);
|
2013-10-25 08:14:36 +04:00
|
|
|
mPendingTextTracks->RemoveTextTrack(aTextTrack);
|
|
|
|
if (aPendingListOnly) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-21 18:15:00 +04:00
|
|
|
mTextTracks->RemoveTextTrack(aTextTrack);
|
2016-06-01 08:35:58 +03:00
|
|
|
// Remove the cues in mNewCues belong to aTextTrack.
|
|
|
|
TextTrackCueList* removeCueList = aTextTrack->GetCues();
|
|
|
|
if (removeCueList) {
|
2019-03-15 02:24:32 +03:00
|
|
|
WEBVTT_LOGV("RemoveTextTrack removeCuesNum=%d",
|
2016-08-25 05:58:49 +03:00
|
|
|
removeCueList->Length());
|
2016-06-01 08:35:58 +03:00
|
|
|
for (uint32_t i = 0; i < removeCueList->Length(); ++i) {
|
2016-06-01 10:13:43 +03:00
|
|
|
mNewCues->RemoveCue(*((*removeCueList)[i]));
|
2016-06-01 08:35:58 +03:00
|
|
|
}
|
2019-03-08 06:12:42 +03:00
|
|
|
TimeMarchesOn();
|
2016-06-01 08:35:58 +03:00
|
|
|
}
|
2013-10-23 22:45:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextTrackManager::DidSeek() {
|
2019-03-15 02:24:32 +03:00
|
|
|
WEBVTT_LOG("DidSeek");
|
2016-06-01 10:13:43 +03:00
|
|
|
if (mMediaElement) {
|
|
|
|
mLastTimeMarchesOnCalled = mMediaElement->CurrentTime();
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOGV("DidSeek set mLastTimeMarchesOnCalled %lf",
|
|
|
|
mLastTimeMarchesOnCalled);
|
2016-06-01 10:13:43 +03:00
|
|
|
}
|
2016-06-01 08:35:56 +03:00
|
|
|
mHasSeeked = true;
|
2013-10-23 22:45:00 +04:00
|
|
|
}
|
|
|
|
|
2013-12-12 21:02:17 +04:00
|
|
|
void TextTrackManager::UpdateCueDisplay() {
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOG("UpdateCueDisplay");
|
2016-07-18 10:39:14 +03:00
|
|
|
mUpdateCueDisplayDispatched = false;
|
|
|
|
|
2017-08-09 12:30:52 +03:00
|
|
|
if (!mMediaElement || !mTextTracks || IsShutdown()) {
|
2014-04-07 21:58:38 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:02:17 +04:00
|
|
|
nsIFrame* frame = mMediaElement->GetPrimaryFrame();
|
|
|
|
nsVideoFrame* videoFrame = do_QueryFrame(frame);
|
|
|
|
if (!videoFrame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> overlay = videoFrame->GetCaptionOverlay();
|
2016-06-27 06:22:15 +03:00
|
|
|
nsCOMPtr<nsIContent> controls = videoFrame->GetVideoControls();
|
2018-09-14 01:38:43 +03:00
|
|
|
if (!overlay) {
|
2013-12-12 21:02:17 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-22 06:30:22 +03:00
|
|
|
nsTArray<RefPtr<TextTrackCue>> showingCues;
|
|
|
|
mTextTracks->GetShowingCues(showingCues);
|
2013-12-12 21:02:17 +04:00
|
|
|
|
2017-05-22 06:30:22 +03:00
|
|
|
if (showingCues.Length() > 0) {
|
2019-03-15 02:24:32 +03:00
|
|
|
WEBVTT_LOG("UpdateCueDisplay, processCues, showingCuesNum=%zu",
|
|
|
|
showingCues.Length());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsVariantCC> jsCues = new nsVariantCC();
|
2013-12-12 21:02:17 +04:00
|
|
|
|
|
|
|
jsCues->SetAsArray(nsIDataType::VTYPE_INTERFACE, &NS_GET_IID(EventTarget),
|
2017-05-22 06:30:22 +03:00
|
|
|
showingCues.Length(),
|
|
|
|
static_cast<void*>(showingCues.Elements()));
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* window = mMediaElement->OwnerDoc()->GetInnerWindow();
|
2013-12-12 21:02:17 +04:00
|
|
|
if (window) {
|
2016-06-27 06:22:15 +03:00
|
|
|
sParserWrapper->ProcessCues(window, jsCues, overlay, controls);
|
2013-12-12 21:02:17 +04:00
|
|
|
}
|
|
|
|
} else if (overlay->Length() > 0) {
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOG("UpdateCueDisplay EmptyString");
|
2013-12-12 21:02:17 +04:00
|
|
|
nsContentUtils::SetNodeTextContent(overlay, EmptyString(), true);
|
|
|
|
}
|
2013-10-23 22:45:00 +04:00
|
|
|
}
|
|
|
|
|
2016-06-02 11:08:32 +03:00
|
|
|
void TextTrackManager::NotifyCueAdded(TextTrackCue& aCue) {
|
2019-03-15 02:24:32 +03:00
|
|
|
WEBVTT_LOG("NotifyCueAdded, cue=%p", &aCue);
|
2014-04-07 21:58:38 +04:00
|
|
|
if (mNewCues) {
|
|
|
|
mNewCues->AddCue(aCue);
|
|
|
|
}
|
2019-03-08 06:12:42 +03:00
|
|
|
TimeMarchesOn();
|
2016-07-11 05:51:08 +03:00
|
|
|
ReportTelemetryForCue();
|
2016-06-01 08:35:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextTrackManager::NotifyCueRemoved(TextTrackCue& aCue) {
|
2019-03-15 02:24:32 +03:00
|
|
|
WEBVTT_LOG("NotifyCueRemoved, cue=%p", &aCue);
|
2016-06-01 08:35:58 +03:00
|
|
|
if (mNewCues) {
|
2016-06-01 10:13:43 +03:00
|
|
|
mNewCues->RemoveCue(aCue);
|
2016-06-01 08:35:58 +03:00
|
|
|
}
|
2019-03-08 06:12:42 +03:00
|
|
|
TimeMarchesOn();
|
2019-03-05 22:48:31 +03:00
|
|
|
DispatchUpdateCueDisplay();
|
2014-01-06 22:03:50 +04:00
|
|
|
}
|
|
|
|
|
2013-10-25 08:14:36 +04:00
|
|
|
void TextTrackManager::PopulatePendingList() {
|
2014-04-07 21:58:38 +04:00
|
|
|
if (!mTextTracks || !mPendingTextTracks || !mMediaElement) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-25 08:14:36 +04:00
|
|
|
uint32_t len = mTextTracks->Length();
|
|
|
|
bool dummy;
|
|
|
|
for (uint32_t index = 0; index < len; ++index) {
|
|
|
|
TextTrack* ttrack = mTextTracks->IndexedGetter(index, dummy);
|
|
|
|
if (ttrack && ttrack->Mode() != TextTrackMode::Disabled &&
|
2014-03-13 22:29:32 +04:00
|
|
|
ttrack->ReadyState() == TextTrackReadyState::Loading) {
|
2014-02-27 21:47:23 +04:00
|
|
|
mPendingTextTracks->AddTextTrack(ttrack,
|
|
|
|
CompareTextTracks(mMediaElement));
|
2013-10-25 08:14:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-07 23:42:33 +04:00
|
|
|
void TextTrackManager::AddListeners() {
|
|
|
|
if (mMediaElement) {
|
|
|
|
mMediaElement->AddEventListener(NS_LITERAL_STRING("resizevideocontrols"),
|
|
|
|
this, false, false);
|
2016-06-27 06:22:15 +03:00
|
|
|
mMediaElement->AddEventListener(NS_LITERAL_STRING("seeked"), this, false,
|
|
|
|
false);
|
|
|
|
mMediaElement->AddEventListener(NS_LITERAL_STRING("controlbarchange"), this,
|
|
|
|
false, true);
|
2014-04-07 23:42:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:40:00 +04:00
|
|
|
void TextTrackManager::HonorUserPreferencesForTrackSelection() {
|
2014-04-07 21:58:38 +04:00
|
|
|
if (performedTrackSelection || !mTextTracks) {
|
2014-03-01 00:40:00 +04:00
|
|
|
return;
|
|
|
|
}
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOG("HonorUserPreferencesForTrackSelection");
|
2014-03-01 00:40:00 +04:00
|
|
|
TextTrackKind ttKinds[] = {TextTrackKind::Captions, TextTrackKind::Subtitles};
|
|
|
|
|
|
|
|
// Steps 1 - 3: Perform automatic track selection for different TextTrack
|
|
|
|
// Kinds.
|
|
|
|
PerformTrackSelection(ttKinds, ArrayLength(ttKinds));
|
|
|
|
PerformTrackSelection(TextTrackKind::Descriptions);
|
|
|
|
PerformTrackSelection(TextTrackKind::Chapters);
|
|
|
|
|
|
|
|
// Step 4: Set all TextTracks with a kind of metadata that are disabled
|
|
|
|
// to hidden.
|
|
|
|
for (uint32_t i = 0; i < mTextTracks->Length(); i++) {
|
|
|
|
TextTrack* track = (*mTextTracks)[i];
|
|
|
|
if (track->Kind() == TextTrackKind::Metadata && TrackIsDefault(track) &&
|
|
|
|
track->Mode() == TextTrackMode::Disabled) {
|
|
|
|
track->SetMode(TextTrackMode::Hidden);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
performedTrackSelection = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextTrackManager::TrackIsDefault(TextTrack* aTextTrack) {
|
|
|
|
HTMLTrackElement* trackElement = aTextTrack->GetTrackElement();
|
|
|
|
if (!trackElement) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return trackElement->Default();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextTrackManager::PerformTrackSelection(TextTrackKind aTextTrackKind) {
|
|
|
|
TextTrackKind ttKinds[] = {aTextTrackKind};
|
|
|
|
PerformTrackSelection(ttKinds, ArrayLength(ttKinds));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextTrackManager::PerformTrackSelection(TextTrackKind aTextTrackKinds[],
|
|
|
|
uint32_t size) {
|
|
|
|
nsTArray<TextTrack*> candidates;
|
|
|
|
GetTextTracksOfKinds(aTextTrackKinds, size, candidates);
|
|
|
|
|
|
|
|
// Step 3: If any TextTracks in candidates are showing then abort these steps.
|
|
|
|
for (uint32_t i = 0; i < candidates.Length(); i++) {
|
|
|
|
if (candidates[i]->Mode() == TextTrackMode::Showing) {
|
2016-12-16 06:16:31 +03:00
|
|
|
WEBVTT_LOGV("PerformTrackSelection Showing return kind %d",
|
|
|
|
static_cast<int>(candidates[i]->Kind()));
|
2014-03-01 00:40:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 4: Honor user preferences for track selection, otherwise, set the
|
|
|
|
// first TextTrack in candidates with a default attribute to showing.
|
|
|
|
// TODO: Bug 981691 - Honor user preferences for text track selection.
|
|
|
|
for (uint32_t i = 0; i < candidates.Length(); i++) {
|
|
|
|
if (TrackIsDefault(candidates[i]) &&
|
|
|
|
candidates[i]->Mode() == TextTrackMode::Disabled) {
|
|
|
|
candidates[i]->SetMode(TextTrackMode::Showing);
|
2016-12-16 06:16:31 +03:00
|
|
|
WEBVTT_LOGV("PerformTrackSelection set Showing kind %d",
|
|
|
|
static_cast<int>(candidates[i]->Kind()));
|
2014-03-01 00:40:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextTrackManager::GetTextTracksOfKinds(TextTrackKind aTextTrackKinds[],
|
|
|
|
uint32_t size,
|
|
|
|
nsTArray<TextTrack*>& aTextTracks) {
|
|
|
|
for (uint32_t i = 0; i < size; i++) {
|
|
|
|
GetTextTracksOfKind(aTextTrackKinds[i], aTextTracks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextTrackManager::GetTextTracksOfKind(TextTrackKind aTextTrackKind,
|
|
|
|
nsTArray<TextTrack*>& aTextTracks) {
|
2014-04-07 21:58:38 +04:00
|
|
|
if (!mTextTracks) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-01 00:40:00 +04:00
|
|
|
for (uint32_t i = 0; i < mTextTracks->Length(); i++) {
|
|
|
|
TextTrack* textTrack = (*mTextTracks)[i];
|
|
|
|
if (textTrack->Kind() == aTextTrackKind) {
|
|
|
|
aTextTracks.AppendElement(textTrack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-07 23:42:33 +04:00
|
|
|
NS_IMETHODIMP
|
2018-04-20 07:49:29 +03:00
|
|
|
TextTrackManager::HandleEvent(Event* aEvent) {
|
2014-04-07 23:42:33 +04:00
|
|
|
if (!mTextTracks) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString type;
|
|
|
|
aEvent->GetType(type);
|
2016-06-27 06:22:15 +03:00
|
|
|
if (type.EqualsLiteral("resizevideocontrols") ||
|
|
|
|
type.EqualsLiteral("seeked")) {
|
2014-04-07 23:42:33 +04:00
|
|
|
for (uint32_t i = 0; i < mTextTracks->Length(); i++) {
|
|
|
|
((*mTextTracks)[i])->SetCuesDirty();
|
|
|
|
}
|
|
|
|
}
|
2016-06-27 06:22:15 +03:00
|
|
|
|
|
|
|
if (type.EqualsLiteral("controlbarchange")) {
|
|
|
|
UpdateCueDisplay();
|
|
|
|
}
|
|
|
|
|
2014-04-07 23:42:33 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-06-01 08:35:56 +03:00
|
|
|
class SimpleTextTrackEvent : public Runnable {
|
|
|
|
public:
|
|
|
|
friend class CompareSimpleTextTrackEvents;
|
2017-06-12 22:34:10 +03:00
|
|
|
SimpleTextTrackEvent(const nsAString& aEventName, double aTime,
|
|
|
|
TextTrack* aTrack, TextTrackCue* aCue)
|
|
|
|
: Runnable("dom::SimpleTextTrackEvent"),
|
|
|
|
mName(aEventName),
|
|
|
|
mTime(aTime),
|
|
|
|
mTrack(aTrack),
|
|
|
|
mCue(aCue) {}
|
2016-06-01 08:35:56 +03:00
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOGV("SimpleTextTrackEvent cue %p mName %s mTime %lf", mCue.get(),
|
|
|
|
NS_ConvertUTF16toUTF8(mName).get(), mTime);
|
2016-06-01 08:35:56 +03:00
|
|
|
mCue->DispatchTrustedEvent(mName);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-08-28 12:22:20 +03:00
|
|
|
void Dispatch() {
|
|
|
|
if (nsCOMPtr<nsIGlobalObject> global = mCue->GetOwnerGlobal()) {
|
|
|
|
global->Dispatch(TaskCategory::Other, do_AddRef(this));
|
|
|
|
} else {
|
|
|
|
NS_DispatchToMainThread(do_AddRef(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 08:35:56 +03:00
|
|
|
private:
|
|
|
|
nsString mName;
|
|
|
|
double mTime;
|
|
|
|
TextTrack* mTrack;
|
|
|
|
RefPtr<TextTrackCue> mCue;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CompareSimpleTextTrackEvents {
|
|
|
|
private:
|
|
|
|
int32_t TrackChildPosition(SimpleTextTrackEvent* aEvent) const {
|
2016-12-28 10:51:37 +03:00
|
|
|
if (aEvent->mTrack) {
|
|
|
|
HTMLTrackElement* trackElement = aEvent->mTrack->GetTrackElement();
|
|
|
|
if (trackElement) {
|
2018-01-23 16:30:18 +03:00
|
|
|
return mMediaElement->ComputeIndexOf(trackElement);
|
2016-12-28 10:51:37 +03:00
|
|
|
}
|
2016-06-01 08:35:56 +03:00
|
|
|
}
|
2016-12-28 10:51:37 +03:00
|
|
|
return -1;
|
2016-06-01 08:35:56 +03:00
|
|
|
}
|
|
|
|
HTMLMediaElement* mMediaElement;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-01 08:35:56 +03:00
|
|
|
public:
|
|
|
|
explicit CompareSimpleTextTrackEvents(HTMLMediaElement* aMediaElement) {
|
|
|
|
mMediaElement = aMediaElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Equals(SimpleTextTrackEvent* aOne, SimpleTextTrackEvent* aTwo) const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LessThan(SimpleTextTrackEvent* aOne, SimpleTextTrackEvent* aTwo) const {
|
2017-06-13 04:52:27 +03:00
|
|
|
// TimeMarchesOn step 13.1.
|
2016-06-01 08:35:56 +03:00
|
|
|
if (aOne->mTime < aTwo->mTime) {
|
|
|
|
return true;
|
|
|
|
} else if (aOne->mTime > aTwo->mTime) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-13 04:52:27 +03:00
|
|
|
// TimeMarchesOn step 13.2 text track cue order.
|
|
|
|
// TextTrack position in TextTrackList
|
|
|
|
TextTrack* t1 = aOne->mTrack;
|
|
|
|
TextTrack* t2 = aTwo->mTrack;
|
|
|
|
MOZ_ASSERT(t1, "CompareSimpleTextTrackEvents t1 is null");
|
|
|
|
MOZ_ASSERT(t2, "CompareSimpleTextTrackEvents t2 is null");
|
|
|
|
if (t1 != t2) {
|
|
|
|
TextTrackList* tList = t1->GetTextTrackList();
|
|
|
|
MOZ_ASSERT(tList, "CompareSimpleTextTrackEvents tList is null");
|
|
|
|
nsTArray<RefPtr<TextTrack>>& textTracks = tList->GetTextTrackArray();
|
|
|
|
auto index1 = textTracks.IndexOf(t1);
|
|
|
|
auto index2 = textTracks.IndexOf(t2);
|
|
|
|
if (index1 < index2) {
|
|
|
|
return true;
|
|
|
|
} else if (index1 > index2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(t1 == t2, "CompareSimpleTextTrackEvents t1 != t2");
|
|
|
|
// c1 and c2 are both belongs to t1.
|
|
|
|
TextTrackCue* c1 = aOne->mCue;
|
|
|
|
TextTrackCue* c2 = aTwo->mCue;
|
|
|
|
if (c1 != c2) {
|
|
|
|
if (c1->StartTime() < c2->StartTime()) {
|
|
|
|
return true;
|
|
|
|
} else if (c1->StartTime() > c2->StartTime()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (c1->EndTime() < c2->EndTime()) {
|
|
|
|
return true;
|
|
|
|
} else if (c1->EndTime() > c2->EndTime()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextTrackCueList* cueList = t1->GetCues();
|
2019-03-13 08:28:44 +03:00
|
|
|
MOZ_ASSERT(cueList);
|
2017-06-13 04:52:27 +03:00
|
|
|
nsTArray<RefPtr<TextTrackCue>>& cues = cueList->GetCuesArray();
|
|
|
|
auto index1 = cues.IndexOf(c1);
|
|
|
|
auto index2 = cues.IndexOf(c2);
|
|
|
|
if (index1 < index2) {
|
|
|
|
return true;
|
|
|
|
} else if (index1 > index2) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-01 08:35:56 +03:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:52:27 +03:00
|
|
|
// TimeMarchesOn step 13.3.
|
2016-06-01 08:35:56 +03:00
|
|
|
if (aOne->mName.EqualsLiteral("enter") ||
|
|
|
|
aTwo->mName.EqualsLiteral("exit")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TextTrackListInternal {
|
|
|
|
public:
|
|
|
|
void AddTextTrack(TextTrack* aTextTrack,
|
|
|
|
const CompareTextTracks& aCompareTT) {
|
|
|
|
if (!mTextTracks.Contains(aTextTrack)) {
|
|
|
|
mTextTracks.InsertElementSorted(aTextTrack, aCompareTT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint32_t Length() const { return mTextTracks.Length(); }
|
|
|
|
TextTrack* operator[](uint32_t aIndex) {
|
|
|
|
return mTextTracks.SafeElementAt(aIndex, nullptr);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-01 08:35:56 +03:00
|
|
|
private:
|
|
|
|
nsTArray<RefPtr<TextTrack>> mTextTracks;
|
|
|
|
};
|
|
|
|
|
2016-07-18 10:39:14 +03:00
|
|
|
void TextTrackManager::DispatchUpdateCueDisplay() {
|
2017-08-09 12:30:52 +03:00
|
|
|
if (!mUpdateCueDisplayDispatched && !IsShutdown() &&
|
2017-11-21 19:33:20 +03:00
|
|
|
mMediaElement->IsCurrentlyPlaying()) {
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOG("DispatchUpdateCueDisplay");
|
2017-03-27 11:51:17 +03:00
|
|
|
nsPIDOMWindowInner* win = mMediaElement->OwnerDoc()->GetInnerWindow();
|
|
|
|
if (win) {
|
2017-11-04 01:25:38 +03:00
|
|
|
nsGlobalWindowInner::Cast(win)->Dispatch(
|
2017-06-12 22:34:10 +03:00
|
|
|
TaskCategory::Other,
|
|
|
|
NewRunnableMethod("dom::TextTrackManager::UpdateCueDisplay", this,
|
|
|
|
&TextTrackManager::UpdateCueDisplay));
|
2017-03-27 11:51:17 +03:00
|
|
|
mUpdateCueDisplayDispatched = true;
|
|
|
|
}
|
2016-07-18 10:39:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 08:35:56 +03:00
|
|
|
void TextTrackManager::DispatchTimeMarchesOn() {
|
|
|
|
// Run the algorithm if no previous instance is still running, otherwise
|
|
|
|
// enqueue the current playback position and whether only that changed
|
|
|
|
// through its usual monotonic increase during normal playback; current
|
|
|
|
// executing call upon completion will check queue for further 'work'.
|
2017-08-09 12:30:52 +03:00
|
|
|
if (!mTimeMarchesOnDispatched && !IsShutdown() &&
|
2017-11-21 19:33:20 +03:00
|
|
|
mMediaElement->IsCurrentlyPlaying()) {
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOG("DispatchTimeMarchesOn");
|
2017-03-27 11:51:17 +03:00
|
|
|
nsPIDOMWindowInner* win = mMediaElement->OwnerDoc()->GetInnerWindow();
|
|
|
|
if (win) {
|
2017-11-04 01:25:38 +03:00
|
|
|
nsGlobalWindowInner::Cast(win)->Dispatch(
|
2017-06-12 22:34:10 +03:00
|
|
|
TaskCategory::Other,
|
|
|
|
NewRunnableMethod("dom::TextTrackManager::TimeMarchesOn", this,
|
|
|
|
&TextTrackManager::TimeMarchesOn));
|
2017-03-27 11:51:17 +03:00
|
|
|
mTimeMarchesOnDispatched = true;
|
|
|
|
}
|
2016-06-01 08:35:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#time-marches-on
|
|
|
|
void TextTrackManager::TimeMarchesOn() {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2019-03-08 06:12:42 +03:00
|
|
|
mTimeMarchesOnDispatched = false;
|
|
|
|
|
2018-12-13 23:04:46 +03:00
|
|
|
CycleCollectedJSContext* context = CycleCollectedJSContext::Get();
|
|
|
|
if (context && context->IsInStableOrMetaStableState()) {
|
|
|
|
// FireTimeUpdate can be called while at stable state following a
|
|
|
|
// current position change which triggered a state watcher in MediaDecoder
|
|
|
|
// (see bug 1443429).
|
|
|
|
// TimeMarchesOn() will modify JS attributes which is forbidden while in
|
|
|
|
// stable state. So we dispatch a task to perform such operation later
|
|
|
|
// instead.
|
|
|
|
DispatchTimeMarchesOn();
|
|
|
|
return;
|
|
|
|
}
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOG("TimeMarchesOn");
|
2016-06-01 08:35:56 +03:00
|
|
|
|
2016-09-29 12:47:33 +03:00
|
|
|
// Early return if we don't have any TextTracks or shutting down.
|
2017-08-09 12:30:52 +03:00
|
|
|
if (!mTextTracks || mTextTracks->Length() == 0 || IsShutdown()) {
|
2016-06-20 10:58:58 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-01 08:35:56 +03:00
|
|
|
nsISupports* parentObject = mMediaElement->OwnerDoc()->GetParentObject();
|
|
|
|
if (NS_WARN_IF(!parentObject)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(parentObject);
|
|
|
|
|
2016-06-01 10:13:43 +03:00
|
|
|
if (mMediaElement &&
|
2016-06-01 12:37:26 +03:00
|
|
|
(!(mMediaElement->GetPlayedOrSeeked()) || mMediaElement->Seeking())) {
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOG("TimeMarchesOn seeking or post return");
|
2016-06-01 08:35:58 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-01 08:35:56 +03:00
|
|
|
// Step 3.
|
|
|
|
double currentPlaybackTime = mMediaElement->CurrentTime();
|
|
|
|
bool hasNormalPlayback = !mHasSeeked;
|
|
|
|
mHasSeeked = false;
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOG(
|
|
|
|
"TimeMarchesOn mLastTimeMarchesOnCalled %lf currentPlaybackTime %lf "
|
|
|
|
"hasNormalPlayback %d",
|
|
|
|
mLastTimeMarchesOnCalled, currentPlaybackTime, hasNormalPlayback);
|
2016-06-01 08:35:56 +03:00
|
|
|
|
|
|
|
// Step 1, 2.
|
|
|
|
RefPtr<TextTrackCueList> currentCues = new TextTrackCueList(window);
|
|
|
|
RefPtr<TextTrackCueList> otherCues = new TextTrackCueList(window);
|
|
|
|
bool dummy;
|
|
|
|
for (uint32_t index = 0; index < mTextTracks->Length(); ++index) {
|
|
|
|
TextTrack* ttrack = mTextTracks->IndexedGetter(index, dummy);
|
2019-03-13 08:33:30 +03:00
|
|
|
if (ttrack && ttrack->Mode() != TextTrackMode::Disabled) {
|
2016-06-01 08:35:58 +03:00
|
|
|
// TODO: call GetCueListByTimeInterval on mNewCues?
|
2019-03-07 22:21:22 +03:00
|
|
|
ttrack->GetCurrentCueList(currentCues);
|
2016-06-01 08:35:56 +03:00
|
|
|
}
|
|
|
|
}
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOGV("TimeMarchesOn currentCues %d", currentCues->Length());
|
2016-06-01 08:35:56 +03:00
|
|
|
// Populate otherCues with 'non-active" cues.
|
|
|
|
if (hasNormalPlayback) {
|
2016-06-20 10:58:58 +03:00
|
|
|
if (currentPlaybackTime < mLastTimeMarchesOnCalled) {
|
|
|
|
// TODO: Add log and find the root cause why the
|
|
|
|
// playback position goes backward.
|
|
|
|
mLastTimeMarchesOnCalled = currentPlaybackTime;
|
|
|
|
}
|
2016-06-01 08:35:56 +03:00
|
|
|
media::Interval<double> interval(mLastTimeMarchesOnCalled,
|
|
|
|
currentPlaybackTime);
|
|
|
|
otherCues = mNewCues->GetCueListByTimeInterval(interval);
|
2018-11-30 13:46:48 +03:00
|
|
|
;
|
2016-06-01 08:35:56 +03:00
|
|
|
} else {
|
|
|
|
// Seek case. Put the mLastActiveCues into otherCues.
|
|
|
|
otherCues = mLastActiveCues;
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < currentCues->Length(); ++i) {
|
|
|
|
TextTrackCue* cue = (*currentCues)[i];
|
2016-06-01 10:13:43 +03:00
|
|
|
otherCues->RemoveCue(*cue);
|
2016-06-01 08:35:56 +03:00
|
|
|
}
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOGV("TimeMarchesOn otherCues %d", otherCues->Length());
|
2016-06-01 08:35:56 +03:00
|
|
|
// Step 4.
|
|
|
|
RefPtr<TextTrackCueList> missedCues = new TextTrackCueList(window);
|
|
|
|
if (hasNormalPlayback) {
|
|
|
|
for (uint32_t i = 0; i < otherCues->Length(); ++i) {
|
|
|
|
TextTrackCue* cue = (*otherCues)[i];
|
|
|
|
if (cue->StartTime() >= mLastTimeMarchesOnCalled &&
|
|
|
|
cue->EndTime() <= currentPlaybackTime) {
|
|
|
|
missedCues->AddCue(*cue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOGV("TimeMarchesOn missedCues %d", missedCues->Length());
|
2016-06-01 08:35:56 +03:00
|
|
|
// Step 5. Empty now.
|
|
|
|
// TODO: Step 6: fire timeupdate?
|
|
|
|
|
|
|
|
// Step 7. Abort steps if condition 1, 2, 3 are satisfied.
|
|
|
|
// 1. All of the cues in current cues have their active flag set.
|
|
|
|
// 2. None of the cues in other cues have their active flag set.
|
|
|
|
// 3. Missed cues is empty.
|
|
|
|
bool c1 = true;
|
|
|
|
for (uint32_t i = 0; i < currentCues->Length(); ++i) {
|
|
|
|
if (!(*currentCues)[i]->GetActive()) {
|
|
|
|
c1 = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool c2 = true;
|
|
|
|
for (uint32_t i = 0; i < otherCues->Length(); ++i) {
|
|
|
|
if ((*otherCues)[i]->GetActive()) {
|
|
|
|
c2 = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool c3 = (missedCues->Length() == 0);
|
|
|
|
if (c1 && c2 && c3) {
|
|
|
|
mLastTimeMarchesOnCalled = currentPlaybackTime;
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOG("TimeMarchesOn step 7 return, mLastTimeMarchesOnCalled %lf",
|
|
|
|
mLastTimeMarchesOnCalled);
|
2016-06-01 08:35:56 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 8. Respect PauseOnExit flag if not seek.
|
|
|
|
if (hasNormalPlayback) {
|
|
|
|
for (uint32_t i = 0; i < otherCues->Length(); ++i) {
|
|
|
|
TextTrackCue* cue = (*otherCues)[i];
|
|
|
|
if (cue && cue->PauseOnExit() && cue->GetActive()) {
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOG("TimeMarchesOn pause the MediaElement");
|
2016-06-01 08:35:56 +03:00
|
|
|
mMediaElement->Pause();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < missedCues->Length(); ++i) {
|
|
|
|
TextTrackCue* cue = (*missedCues)[i];
|
|
|
|
if (cue && cue->PauseOnExit()) {
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOG("TimeMarchesOn pause the MediaElement");
|
2016-06-01 08:35:56 +03:00
|
|
|
mMediaElement->Pause();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 15.
|
|
|
|
// Sort text tracks in the same order as the text tracks appear
|
|
|
|
// in the media element's list of text tracks, and remove
|
|
|
|
// duplicates.
|
|
|
|
TextTrackListInternal affectedTracks;
|
|
|
|
// Step 13, 14.
|
|
|
|
nsTArray<RefPtr<SimpleTextTrackEvent>> eventList;
|
|
|
|
// Step 9, 10.
|
|
|
|
// For each text track cue in missed cues, prepare an event named
|
|
|
|
// enter for the TextTrackCue object with the cue start time.
|
|
|
|
for (uint32_t i = 0; i < missedCues->Length(); ++i) {
|
|
|
|
TextTrackCue* cue = (*missedCues)[i];
|
|
|
|
if (cue) {
|
|
|
|
SimpleTextTrackEvent* event = new SimpleTextTrackEvent(
|
|
|
|
NS_LITERAL_STRING("enter"), cue->StartTime(), cue->GetTrack(), cue);
|
|
|
|
eventList.InsertElementSorted(
|
|
|
|
event, CompareSimpleTextTrackEvents(mMediaElement));
|
|
|
|
affectedTracks.AddTextTrack(cue->GetTrack(),
|
|
|
|
CompareTextTracks(mMediaElement));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 11, 17.
|
|
|
|
for (uint32_t i = 0; i < otherCues->Length(); ++i) {
|
|
|
|
TextTrackCue* cue = (*otherCues)[i];
|
2016-06-17 12:20:24 +03:00
|
|
|
if (cue->GetActive() || missedCues->IsCueExist(cue)) {
|
2016-06-01 08:35:56 +03:00
|
|
|
double time =
|
|
|
|
cue->StartTime() > cue->EndTime() ? cue->StartTime() : cue->EndTime();
|
|
|
|
SimpleTextTrackEvent* event = new SimpleTextTrackEvent(
|
|
|
|
NS_LITERAL_STRING("exit"), time, cue->GetTrack(), cue);
|
|
|
|
eventList.InsertElementSorted(
|
|
|
|
event, CompareSimpleTextTrackEvents(mMediaElement));
|
|
|
|
affectedTracks.AddTextTrack(cue->GetTrack(),
|
|
|
|
CompareTextTracks(mMediaElement));
|
|
|
|
}
|
|
|
|
cue->SetActive(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 12, 17.
|
|
|
|
for (uint32_t i = 0; i < currentCues->Length(); ++i) {
|
|
|
|
TextTrackCue* cue = (*currentCues)[i];
|
|
|
|
if (!cue->GetActive()) {
|
|
|
|
SimpleTextTrackEvent* event = new SimpleTextTrackEvent(
|
|
|
|
NS_LITERAL_STRING("enter"), cue->StartTime(), cue->GetTrack(), cue);
|
|
|
|
eventList.InsertElementSorted(
|
|
|
|
event, CompareSimpleTextTrackEvents(mMediaElement));
|
|
|
|
affectedTracks.AddTextTrack(cue->GetTrack(),
|
|
|
|
CompareTextTracks(mMediaElement));
|
|
|
|
}
|
|
|
|
cue->SetActive(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire the eventList
|
|
|
|
for (uint32_t i = 0; i < eventList.Length(); ++i) {
|
2017-08-28 12:22:20 +03:00
|
|
|
eventList[i]->Dispatch();
|
2016-06-01 08:35:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Step 16.
|
|
|
|
for (uint32_t i = 0; i < affectedTracks.Length(); ++i) {
|
|
|
|
TextTrack* ttrack = affectedTracks[i];
|
|
|
|
if (ttrack) {
|
2016-07-12 12:41:41 +03:00
|
|
|
ttrack->DispatchAsyncTrustedEvent(NS_LITERAL_STRING("cuechange"));
|
2016-06-01 10:13:43 +03:00
|
|
|
HTMLTrackElement* trackElement = ttrack->GetTrackElement();
|
|
|
|
if (trackElement) {
|
|
|
|
trackElement->DispatchTrackRunnable(NS_LITERAL_STRING("cuechange"));
|
|
|
|
}
|
2016-06-01 08:35:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mLastTimeMarchesOnCalled = currentPlaybackTime;
|
|
|
|
mLastActiveCues = currentCues;
|
2016-06-01 12:37:26 +03:00
|
|
|
|
|
|
|
// Step 18.
|
|
|
|
UpdateCueDisplay();
|
2016-06-01 08:35:56 +03:00
|
|
|
}
|
|
|
|
|
2016-06-08 11:53:30 +03:00
|
|
|
void TextTrackManager::NotifyCueUpdated(TextTrackCue* aCue) {
|
|
|
|
// TODO: Add/Reorder the cue to mNewCues if we have some optimization?
|
2019-03-15 02:24:32 +03:00
|
|
|
WEBVTT_LOG("NotifyCueUpdated, cue=%p", aCue);
|
2019-03-08 06:12:42 +03:00
|
|
|
TimeMarchesOn();
|
2017-05-17 09:57:51 +03:00
|
|
|
// For the case "Texttrack.mode = hidden/showing", if the mode
|
|
|
|
// changing between showing and hidden, TimeMarchesOn
|
|
|
|
// doesn't render the cue. Call DispatchUpdateCueDisplay() explicitly.
|
|
|
|
DispatchUpdateCueDisplay();
|
2016-06-08 11:53:30 +03:00
|
|
|
}
|
|
|
|
|
2016-06-20 10:58:58 +03:00
|
|
|
void TextTrackManager::NotifyReset() {
|
2016-08-25 05:58:49 +03:00
|
|
|
WEBVTT_LOG("NotifyReset");
|
2016-06-20 10:58:58 +03:00
|
|
|
mLastTimeMarchesOnCalled = 0.0;
|
|
|
|
}
|
|
|
|
|
2016-07-11 05:51:08 +03:00
|
|
|
void TextTrackManager::ReportTelemetryForTrack(TextTrack* aTextTrack) const {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aTextTrack);
|
|
|
|
MOZ_ASSERT(mTextTracks->Length() > 0);
|
|
|
|
|
|
|
|
TextTrackKind kind = aTextTrack->Kind();
|
|
|
|
Telemetry::Accumulate(Telemetry::WEBVTT_TRACK_KINDS, uint32_t(kind));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextTrackManager::ReportTelemetryForCue() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mNewCues->IsEmpty() || !mLastActiveCues->IsEmpty());
|
|
|
|
|
|
|
|
if (!mCueTelemetryReported) {
|
|
|
|
Telemetry::Accumulate(Telemetry::WEBVTT_USED_VTT_CUES, 1);
|
|
|
|
mCueTelemetryReported = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 10:27:06 +03:00
|
|
|
bool TextTrackManager::IsLoaded() {
|
|
|
|
return mTextTracks ? mTextTracks->AreTextTracksLoaded() : true;
|
|
|
|
}
|
|
|
|
|
2017-08-09 12:30:52 +03:00
|
|
|
bool TextTrackManager::IsShutdown() const {
|
|
|
|
return (mShutdown || !sParserWrapper);
|
|
|
|
}
|
2017-02-20 10:27:06 +03:00
|
|
|
|
2013-10-23 22:45:00 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|